2016-05-12 46 views
2

解析requirements.txt後,讓我們假設這是PIP模塊列表:如何從其pip需求名稱中檢索模塊的導入名稱?

>>> modules_req = ['beautifulsoup4','django-nose','ujson'] 
>>> for module in modules_req: 
...  __import__(module) 
... 
Traceback (most recent call last): 
    File "<stdin>", line 2, in <module> 
ImportError: No module named beautifulsoup4 

我們怎樣才能retreive這些模塊即bs4beautifulsoup4的導入名稱,併爲django_nosedjango-nose,例如?

+0

請問downvoter可以解釋closevote和downvote的原因嗎? – DhruvPathak

+0

不是downvoter,但爲什麼不直接在'modules_req'中包含模塊導入名稱呢? –

+0

@IronFist,因爲正如我所提到的那樣,它已經從一個requirements.txt文件中解析出來了。上述示例可以是部署前的環境測試。 – DhruvPathak

回答

2

我不知道這是你真正想做的事,但對於已安裝的軟件包,你可能會得到一些里程查詢出來爲pkg_resources包信息的:

$ pip freeze 
beautifulsoup4==4.4.1 

>>> import pkg_resources 
>>> list(
     pkg_resources 
     .get_distribution('beautifulsoup4') 
     ._get_metadata('top_level.txt') 
    ) 
['bs4'] 
+0

適合賬單! – DhruvPathak

相關問題