如何获得在计算机上安装的Python模块列表?
当前回答
pip3 freeze
全局运行此命令-将显示所有全局安装 在虚拟环境上运行此命令-将显示所有本地安装
其他回答
在ipython中,你可以输入“importTab”。 在标准的Python解释器中,您可以输入“help('modules')”。 在命令行中,您可以使用pydoc模块。 在脚本中,调用pkgutil.iter_modules()。
我需要找到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
我比较了五种方法来检索已安装的“模块”,所有这些都是我在这个线程中看到的
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)
作品不考虑Pip版本
在python编辑器或IPython中运行以下命令:
import pkg_resources
installed_packages = {d.project_name: d.version for d in pkg_resources.working_set}
print(installed_packages)
阅读其他答案并将这个组合组合在一起,这是Python中最快和最简单的方法。
找到特定的包
你可以很方便地从词典中获取条目。
安装packages[‘pandas’]>> 1.16.4'
善用Pip List
pip列表将在你的jupyter笔记本中运行,简化了“快速检查” 与其他实用程序(如grep)结合使用(如果已安装) 例如,PIP列表| grep pandas将为您提供当前的pandas版本
安装
pip install pkgutil
Code
import pkgutil
for i in pkgutil.iter_modules(None): # returns a tuple (path, package_name, ispkg_flag)
print(i[1]) #or you can append it to a list
样例输出:
multiprocessing
netrc
nntplib
ntpath
nturl2path
numbers
opcode
pickle
pickletools
pipes
pkgutil
推荐文章
- 证书验证失败:无法获得本地颁发者证书
- 当使用pip3安装包时,“Python中的ssl模块不可用”
- 无法切换Python与pyenv
- Python if not == vs if !=
- 如何从scikit-learn决策树中提取决策规则?
- 为什么在Mac OS X v10.9 (Mavericks)的终端中apt-get功能不起作用?
- 将旋转的xtick标签与各自的xtick对齐
- 为什么元组可以包含可变项?
- 如何合并字典的字典?
- 如何创建类属性?
- 不区分大小写的“in”
- 在Python中获取迭代器中的元素个数
- 解析日期字符串并更改格式
- 使用try和。Python中的if
- 如何在Python中获得所有直接子目录