如何获得在计算机上安装的Python模块列表?
当前回答
pip install pip-chill
pip-chill
其他回答
除了使用pip freeze,我还在虚拟环境中安装了蛋黄。
我比较了五种方法来检索已安装的“模块”,所有这些都是我在这个线程中看到的
iter_modules | help("modules") | builtin_module_names | pip list | working_set | |
---|---|---|---|---|---|
Includes distributions | ❌ | ❌ | ❌ | ✔️ | ✔️ |
Includes modules (No built-in) | ✔️ | ✔️ | ❌ | ❌ | ❌ |
Includes built-in modules | ❌ | ✔️ | ✔️ | ❌ | ❌ |
Includes frozen | ✔️ | ✔️ | ❌ | ❌ | ❌ |
Includes venv | ✔️ | ✔️ | ❌ | ✔️ | ✔️ |
Includes global | ✔️ | ✔️ | ❌ | ✔️ | ✔️ |
Includes editable installs | ✔️ | ✔️ | ❌ | ✔️ | ✔️ |
Includes PyCharm helpers | ✔️ | ❌ | ❌ | ❌ | ❌ |
Lowers capital letters | ❌ | ❌ | ❌ | ❌ | ✔️ |
Time taken (665 modules total) | 53.7 msec | 1.03 sec | 577 nsec | 284 msec | 36.2 usec |
总结
PIP list和working_set是针对发行版的,而不是模块的。 Iter_modules和help("modules")非常相似,最大的区别是Iter_modules不包含内置模块。 PIP list和working_set非常相似,唯一不同的是working_set降低了所有大写字母。 内置模块只包含在help("modules")和builtin_module_names中。
相关的说明
发行版、包和模块通常具有相同的名称,因此很容易将它们误认为另一个。 importlib.util。Find_spec用于模块,区分大小写。 sys。Modules只列出导入的模块。
分布
我说分发而不是打包,因为我认为这样会减少误解。一个发行版/包可以包含多个包/模块。
已安装的发行版并不总是可以通过相同的名称导入。例如pip install Pillow是进口PIL。有时,一个发行版甚至可以导入多个模块。
方法(每列按顺序排列)
iter_modules
import pkgutil
{module.name for module in pkgutil.iter_modules()}
帮助("modules")(仅在终端中打印)
help("modules")
builtin_module_names
import sys
set(sys.builtin_module_names)
pip列表(仅在终端打印)
终端PIP清单
working_set
import pkg_resources
{pkg.key for pkg in pkg_resources.working_set}
结论
对于终端,我建议使用help("modules")或python -c "help("modules") "。 在这个帖子中投票最多的答案 对于编程方式,我建议使用iter_modules + builtin_module_names。 这个答案在这个帖子里 然而,这是非常复杂的信息,请看我下面的最小示例。
import sys
import pkgutil
def get_installed_modules_names():
iter_modules = {module.name for module in pkgutil.iter_modules()}
builtin = sys.builtin_module_names
return set.union(iter_modules, builtin)
如果安装了anaconda python发行版,也可以使用
$conda list
除了上述解决方案。
剥猫皮的方法有很多。
最简单的方法是直接在shell中使用pydoc函数: pydoc模块 但要了解更多信息,请使用pip-date工具,该工具还会告诉您安装日期。 PIP安装PIP -date
我需要找到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
推荐文章
- 试图在Windows 10上运行Python时出现“权限被拒绝”
- 如何在Django中设置时区
- 即使模板文件存在,Flask也会引发TemplateNotFound错误
- defaultdict的嵌套defaultdict
- 构造tkinter应用程序的最佳方法?
- 如何在Python中逐行打印字典?
- 当试图运行Python脚本时,“ImportError:没有命名模块”
- pylab和pyplot的区别是什么?
- Argparse:确定使用了哪个子解析器
- django导入错误-没有core.management模块
- 在芹菜中检索队列中的任务列表
- 使用beautifulsoup提取属性值
- 如何禁用标准错误流的日志记录?
- 用Matplotlib在Python中绘制时间
- 类中的Python装饰器