2017-07-28 35 views
0

如何通過運行Python或IPython實例的自省來列出需要的軟件包?在Python中通過自省列出所需的軟件包

我經常從一個非常安裝的conda環境開始開發,該環境具有完整的Anaconda發行版,並且安裝了更多的完整版本。當我希望共享代碼時,我希望能夠爲只包含當前加載在解釋器中的包的conda吐出requirements.txt的pip或environment.yml

我該怎麼做?

+2

建立從加載模塊到包的關係將是一項複雜的任務。一般來說,我建議開始在空的環境下開發。 –

+1

@克勞斯不conda保持這樣的映射? –

+0

很容易獲得加載的模塊的名稱,但它們不一定是conda或pip所需的名稱。許多內置和「私人」模塊將在列表中。 –

回答

3

你可以使用python builtin包modulefinder來測試你的腳本的模塊。例如:

from modulefinder import ModuleFinder 

finder = ModuleFinder() 
finder.run_script('bacon.py') 

print('Loaded modules:') 
for name, mod in finder.modules.items(): 
    print('%s: ' % name, end='') 
    print(','.join(list(mod.globalnames.keys())[:3])) 

print('-'*50) 
print('Modules not imported:') 
print('\n'.join(finder.badmodules.keys())) 
相關問題