2016-05-12 48 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

相关问题