如何获得在计算机上安装的Python模块列表?
当前回答
现在,我自己尝试了这些方法,我得到了广告中所说的:所有的模块。
唉,实际上你不太关心stdlib,你知道安装python会得到什么。
真的,我想要我安装的东西。
令人惊讶的是,实际上效果还不错的是:
pip freeze
返回:
Fabric==0.9.3
apache-libcloud==0.4.0
bzr==2.3b4
distribute==0.6.14
docutils==0.7
greenlet==0.3.1
ipython==0.10.1
iterpipes==0.4
libxml2-python==2.6.21
我说“令人惊讶”是因为包安装工具是人们期望找到这个功能的确切位置,尽管不是在“冻结”的名称下,但python打包是如此奇怪,以至于我惊讶于这个工具是有意义的。Pip 0.8.2, Python 2.7。
其他回答
在windows上,在cmd中输入这个
c:\python\libs>python -m pip freeze
此解决方案主要基于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*******************************)
从外壳开始
ls site-packages
如果没有帮助,你可以这样做。
import sys
import os
for p in sys.path:
print os.listdir( p )
看看会产生什么。
我通常使用pip list来获取包的列表(带有版本)。
当然,这也适用于虚拟环境。要显示只在虚拟环境中安装了什么(而不是全局包),请使用pip list——local。
下面的文档显示了所有可用的pip列表选项,并提供了几个很好的示例。
help('modules')
在Python shell/提示符中。
推荐文章
- 将Pandas或Numpy Nan替换为None以用于MysqlDB
- 使用pandas对同一列进行多个聚合
- 使用Python解析HTML
- django MultiValueDictKeyError错误,我如何处理它
- 如何在for循环期间修改列表条目?
- 我如何在Django中创建一个鼻涕虫?
- 没有名为'django.core.urlresolvers'的模块
- 蟒蛇导出环境文件
- Django - makemigrations -未检测到任何更改
- SQLAlchemy:引擎、连接和会话差异
- 在Python Pandas中删除多个列中的所有重复行
- 更改pandas DataFrame中的特定列名
- 将Pandas多索引转换为列
- 熊猫在每组中获得最高的n个记录
- 熊猫数据帧得到每组的第一行