如何获得在计算机上安装的Python模块列表?
当前回答
如果安装了anaconda python发行版,也可以使用
$conda list
除了上述解决方案。
其他回答
从pip 10开始,接受的答案将不再有效。开发团队已经删除了对get_installed_distribution例程的访问。在setuptools中有一个替代函数可以做同样的事情。下面是一个适用于pip 10的替代版本:
import pkg_resources
installed_packages = pkg_resources.working_set
installed_packages_list = sorted(["%s==%s" % (i.key, i.version)
for i in installed_packages])
print(installed_packages_list)
请让我知道它是否会工作在以前版本的pip,太。
此解决方案主要基于importlib和pkgutil模块,并与CPython 3.4和CPython 3.5一起工作,但不支持CPython 2。
解释
sys。Builtin_module_names -命名所有内置模块(看我的答案在这里) iter_modules() -返回所有可用模块的信息 Importlib.util.find_spec() -返回关于导入模块的信息,如果存在的话 BuiltinImporter -内置模块(文档)的导入器 SourceFileLoader -标准Python模块的导入器(默认扩展名为*.py) ExtensionFileLoader——作为共享库的模块导入器(用C或c++编写)
完整代码
import sys
import os
import shutil
import pkgutil
import importlib
import collections
if sys.version_info.major == 2:
raise NotImplementedError('CPython 2 is not supported yet')
def main():
# name this file (module)
this_module_name = os.path.basename(__file__).rsplit('.')[0]
# dict for loaders with their modules
loaders = collections.OrderedDict()
# names`s of build-in modules
for module_name in sys.builtin_module_names:
# find an information about a module by name
module = importlib.util.find_spec(module_name)
# add a key about a loader in the dict, if not exists yet
if module.loader not in loaders:
loaders[module.loader] = []
# add a name and a location about imported module in the dict
loaders[module.loader].append((module.name, module.origin))
# all available non-build-in modules
for module_name in pkgutil.iter_modules():
# ignore this module
if this_module_name == module_name[1]:
continue
# find an information about a module by name
module = importlib.util.find_spec(module_name[1])
# add a key about a loader in the dict, if not exists yet
loader = type(module.loader)
if loader not in loaders:
loaders[loader] = []
# add a name and a location about imported module in the dict
loaders[loader].append((module.name, module.origin))
# pretty print
line = '-' * shutil.get_terminal_size().columns
for loader, modules in loaders.items():
print('{0}\n{1}: {2}\n{0}'.format(line, len(modules), loader))
for module in modules:
print('{0:30} | {1}'.format(module[0], module[1]))
if __name__ == '__main__':
main()
使用
对于CPython3.5(截断)
$ python3.5 python_modules_info.py
------------------------------------------------------------------------------------------------------------------------------------------------------------------------
30: <class '_frozen_importlib.BuiltinImporter'>
------------------------------------------------------------------------------------------------------------------------------------------------------------------------
_ast | built-in
_codecs | built-in
_collections | built-in
_functools | built-in
_imp | None
_io | built-in
_locale | built-in
_operator | built-in
_signal | built-in
_sre | built-in
_stat | built-in
_string | built-in
_symtable | built-in
_thread | built-in
(****************************truncated*******************************)
------------------------------------------------------------------------------------------------------------------------------------------------------------------------
227: <class '_frozen_importlib_external.SourceFileLoader'>
------------------------------------------------------------------------------------------------------------------------------------------------------------------------
__future__ | /usr/local/lib/python3.5/__future__.py
_bootlocale | /usr/local/lib/python3.5/_bootlocale.py
_collections_abc | /usr/local/lib/python3.5/_collections_abc.py
_compat_pickle | /usr/local/lib/python3.5/_compat_pickle.py
_compression | /usr/local/lib/python3.5/_compression.py
_dummy_thread | /usr/local/lib/python3.5/_dummy_thread.py
_markupbase | /usr/local/lib/python3.5/_markupbase.py
_osx_support | /usr/local/lib/python3.5/_osx_support.py
_pydecimal | /usr/local/lib/python3.5/_pydecimal.py
_pyio | /usr/local/lib/python3.5/_pyio.py
_sitebuiltins | /usr/local/lib/python3.5/_sitebuiltins.py
(****************************truncated*******************************)
------------------------------------------------------------------------------------------------------------------------------------------------------------------------
64: <class '_frozen_importlib_external.ExtensionFileLoader'>
------------------------------------------------------------------------------------------------------------------------------------------------------------------------
_bisect | /usr/local/lib/python3.5/lib-dynload/_bisect.cpython-35m-x86_64-linux-gnu.so
_bz2 | /usr/local/lib/python3.5/lib-dynload/_bz2.cpython-35m-x86_64-linux-gnu.so
_codecs_cn | /usr/local/lib/python3.5/lib-dynload/_codecs_cn.cpython-35m-x86_64-linux-gnu.so
_codecs_hk | /usr/local/lib/python3.5/lib-dynload/_codecs_hk.cpython-35m-x86_64-linux-gnu.so
_codecs_iso2022 | /usr/local/lib/python3.5/lib-dynload/_codecs_iso2022.cpython-35m-x86_64-linux-gnu.so
(****************************truncated*******************************)
对于CPython3.4(截断)
$ python3.4 python_modules_info.py
------------------------------------------------------------------------------------------------------------------------------------------------------------------------
54: <class '_frozen_importlib.BuiltinImporter'>
------------------------------------------------------------------------------------------------------------------------------------------------------------------------
_ast | built-in
_bisect | built-in
_codecs | built-in
_collections | built-in
_datetime | built-in
_elementtree | built-in
_functools | built-in
_heapq | built-in
_imp | None
_io | built-in
_locale | built-in
_md5 | built-in
_operator | built-in
_pickle | built-in
_posixsubprocess | built-in
_random | built-in
(****************************truncated*******************************)
------------------------------------------------------------------------------------------------------------------------------------------------------------------------
246: <class '_frozen_importlib.SourceFileLoader'>
------------------------------------------------------------------------------------------------------------------------------------------------------------------------
__future__ | /usr/lib/python3.4/__future__.py
_bootlocale | /usr/lib/python3.4/_bootlocale.py
_collections_abc | /usr/lib/python3.4/_collections_abc.py
_compat_pickle | /usr/lib/python3.4/_compat_pickle.py
_dummy_thread | /usr/lib/python3.4/_dummy_thread.py
_markupbase | /usr/lib/python3.4/_markupbase.py
_osx_support | /usr/lib/python3.4/_osx_support.py
_pyio | /usr/lib/python3.4/_pyio.py
(****************************truncated*******************************)
------------------------------------------------------------------------------------------------------------------------------------------------------------------------
44: <class '_frozen_importlib.ExtensionFileLoader'>
------------------------------------------------------------------------------------------------------------------------------------------------------------------------
_bz2 | /usr/lib/python3.4/lib-dynload/_bz2.cpython-34m-x86_64-linux-gnu.so
_codecs_cn | /usr/lib/python3.4/lib-dynload/_codecs_cn.cpython-34m-x86_64-linux-gnu.so
_codecs_hk | /usr/lib/python3.4/lib-dynload/_codecs_hk.cpython-34m-x86_64-linux-gnu.so
_codecs_iso2022 | /usr/lib/python3.4/lib-dynload/_codecs_iso2022.cpython-34m-x86_64-linux-gnu.so
_codecs_jp | /usr/lib/python3.4/lib-dynload/_codecs_jp.cpython-34m-x86_64-linux-gnu.so
_codecs_kr | /usr/lib/python3.4/lib-dynload/_codecs_kr.cpython-34m-x86_64-linux-gnu.so
_codecs_tw | /usr/lib/python3.4/lib-dynload/_codecs_tw.cpython-34m-x86_64-linux-gnu.so
_crypt | /usr/lib/python3.4/lib-dynload/_crypt.cpython-34m-x86_64-linux-gnu.so
(****************************truncated*******************************)
作品不考虑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版本
我在OS x上遇到了一个自定义安装的python 2.7。它需要X11来列出已安装的模块(使用help和pydoc)。
为了能够在不安装X11的情况下列出所有模块,我运行pydoc作为http-server,即:
pydoc -p 12345
然后,可以将Safari定向到http://localhost:12345/以查看所有模块。
警告:Adam Matan不鼓励在pip > 10.0中使用此功能。另外,请阅读下面@sinoroc的评论
这是受到Adam Matan的回答(公认的答案)的启发:
import tabulate
try:
from pip import get_installed_distributions
except:
from pip._internal.utils.misc import get_installed_distributions
tabpackages = []
for _, package in sorted([('%s %s' % (i.location, i.key), i) for i in get_installed_distributions()]):
tabpackages.append([package.location, package.key, package.version])
print(tabulate.tabulate(tabpackages))
然后打印出一个表格的形式
19:33 pi@rpi-v3 [iot-wifi-2] ~/python$ python installed_packages.py
------------------------------------------- -------------- ------
/home/pi/.local/lib/python2.7/site-packages enum-compat 0.0.2
/home/pi/.local/lib/python2.7/site-packages enum34 1.1.6
/home/pi/.local/lib/python2.7/site-packages pexpect 4.2.1
/home/pi/.local/lib/python2.7/site-packages ptyprocess 0.5.2
/home/pi/.local/lib/python2.7/site-packages pygatt 3.2.0
/home/pi/.local/lib/python2.7/site-packages pyserial 3.4
/usr/local/lib/python2.7/dist-packages bluepy 1.1.1
/usr/local/lib/python2.7/dist-packages click 6.7
/usr/local/lib/python2.7/dist-packages click-datetime 0.2
/usr/local/lib/python2.7/dist-packages construct 2.8.21
/usr/local/lib/python2.7/dist-packages pyaudio 0.2.11
/usr/local/lib/python2.7/dist-packages tabulate 0.8.2
------------------------------------------- -------------- ------
这可以让您轻松地辨别安装了哪些包,哪些包没有安装sudo。
顺便说一句:我注意到,当我通过sudo安装一个包一次而不安装一次时,一个包优先,这样另一个包就不会被列出(只显示一个位置)。我相信只有本地目录中的文件才会被列出。这是可以改进的。
推荐文章
- 如何在Python中使用方法重载?
- 在Python中提取文件路径(目录)的一部分
- 如何安装没有根访问权限的python模块?
- 尝试模拟datetime.date.today(),但不工作
- 将行添加到数组
- 如何在Python中直接获得字典键作为变量(而不是通过从值搜索)?
- Python:为什么functools。部分有必要吗?
- 如何用python timeit对代码段进行性能测试?
- Python迭代器中的has_next ?
- ConfigParser中的列表
- 由于环境错误无法安装包:[Errno 13]
- 如何测试一个字符串是否包含列表中的一个子字符串,在熊猫?
- 'datetime'模块没有'strptime'属性
- 如何将字典保存到文件?
- 如何在Python中绘制网格?