如何获得在计算机上安装的Python模块列表?
当前回答
pip install pip-chill
pip-chill
其他回答
如果以上这些似乎都没有帮助,那么在我的环境中,系统升级被打破了,我无法升级pip。虽然它不会给你一个准确的列表,但你可以通过查看你的env>lib>python(版本在这里)>site-packages>来了解安装了哪些库。在这里,您可以很好地了解已安装的模块。
我需要找到AWS Lambda中默认可用的软件包的特定版本。我这样做是从这个页面的想法混搭。我要为子孙后代分享。
import pkgutil
__version__ = '0.1.1'
def get_ver(name):
try:
return str(__import__(name).__version__)
except:
return None
def lambda_handler(event, context):
return {
'statusCode': 200,
'body': [{
'path': m.module_finder.path,
'name': m.name,
'version': get_ver(m.name),
} for m in list(pkgutil.iter_modules())
#if m.module_finder.path == "/var/runtime" # Uncomment this if you only care about a certain path
],
}
我发现提供的boto3库已经过时了,代码失败并不是我的错。我只需要添加boto3和botocore到我的项目。但如果没有这个,我可能会一直在想我的代码很糟糕。
{
"statusCode": 200,
"body": [
{
"path": "/var/task",
"name": "lambda_function",
"version": "0.1.1"
},
{
"path": "/var/runtime",
"name": "bootstrap",
"version": null
},
{
"path": "/var/runtime",
"name": "boto3",
"version": "1.9.42"
},
{
"path": "/var/runtime",
"name": "botocore",
"version": "1.12.42"
},
{
"path": "/var/runtime",
"name": "dateutil",
"version": "2.7.5"
},
{
"path": "/var/runtime",
"name": "docutils",
"version": "0.14"
},
{
"path": "/var/runtime",
"name": "jmespath",
"version": "0.9.3"
},
{
"path": "/var/runtime",
"name": "lambda_runtime_client",
"version": null
},
{
"path": "/var/runtime",
"name": "lambda_runtime_exception",
"version": null
},
{
"path": "/var/runtime",
"name": "lambda_runtime_marshaller",
"version": null
},
{
"path": "/var/runtime",
"name": "s3transfer",
"version": "0.1.13"
},
{
"path": "/var/runtime",
"name": "six",
"version": "1.11.0"
},
{
"path": "/var/runtime",
"name": "test_bootstrap",
"version": null
},
{
"path": "/var/runtime",
"name": "test_lambda_runtime_client",
"version": null
},
{
"path": "/var/runtime",
"name": "test_lambda_runtime_marshaller",
"version": null
},
{
"path": "/var/runtime",
"name": "urllib3",
"version": "1.24.1"
},
{
"path": "/var/lang/lib/python3.7",
"name": "__future__",
"version": null
},
...
我所发现的也与他们官方公布的有所不同。在撰写本文时:
操作系统-亚马逊Linux AMI - amzn-ami-hvm-2017.03.1.20170812-x86_64-gp2 Linux内核- 4.14.77-70.59.amzn1.x86_64 AWS SDK for JavaScript - 2.290.0\ Python SDK (Boto 3) - 3-1.7.74 botocore-1.10.74
PIP冻结可以找到所有的包,但是你可以简单地写下面的命令来列出python包所在的所有路径。
>>> import site; site.getsitepackages()
['/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages']
我在OS x上遇到了一个自定义安装的python 2.7。它需要X11来列出已安装的模块(使用help和pydoc)。
为了能够在不安装X11的情况下列出所有模块,我运行pydoc作为http-server,即:
pydoc -p 12345
然后,可以将Safari定向到http://localhost:12345/以查看所有模块。
使用pkgutil.iter_modules进行非常简单的搜索
from pkgutil import iter_modules
a=iter_modules()
while True:
try: x=a.next()
except: break
if 'searchstr' in x[1]: print x[1]