如何获取本地安装的 Python 模块的列表? [英] How can I get a list of locally installed Python modules?

查看:17
本文介绍了如何获取本地安装的 Python 模块的列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获取 Python 模块的列表,这些模块在我的 Python 安装(UNIX 服务器)中.

如何获取计算机中安装的 Python 模块列表?

解决方案

解决方案

不要与 pip > 10.0 一起使用!

我从 Python 脚本中获得类似 pip freeze 的列表的 50 美分:

import pipinstalled_pa​​ckages = pip.get_installed_distributions()installed_pa​​ckages_list = sorted(["%s==%s" % (i.key, i.version)对于我在 installed_pa​​ckages])打印(installed_pa​​ckages_list)

作为(太长)单衬:

sorted(["%s==%s" % (i.key, i.version) for i in pip.get_installed_distributions()])

给予:

['behave==1.2.4', 'enum34==1.0', 'flask==0.10.1', 'itsdangerous==0.24','jinja2==2.7.2', 'jsonschema==2.3.0', 'markupsafe==0.23', 'nose==1.3.3','解析类型==0.3.4'、'解析==1.6.4'、'漂亮的==0.7.2'、'请求==2.3.0'、'six==1.6.1', 'vioozer-metadata==0.1', 'vioozer-users-server==0.1','werkzeug==0.9.4']

范围

此解决方案适用于系统范围或虚拟环境范围,涵盖由 setuptoolspip 和 (上帝保佑) easy_install.

我的用例

我将这个调用的结果添加到了我的烧瓶服务器,所以当我用 http://example.com/exampleServer/environment 调用它时,我得到了安装在服务器 virtualenv 上的包列表.它使调试变得更加容易.

注意事项

我注意到这种技术的一个奇怪行为 - 当 Python 解释器在与 setup.py 文件相同的目录中被调用时,它没有列出由 setup 安装的包.py.

重现步骤:

创建虚拟环境

$ cd/tmp$ virtualenv test_envtest_env/bin/python 中的新 python 可执行文件安装 setuptools,pip...完成.$ source test_env/bin/activate(test_env) $

使用 setup.py 克隆一个 git repo

(test_env) $ git clone https://github.com/behave/behave.git克隆成行为"...远程:重用现有包:4350,完成.远程:总计 4350(增量 0),重用 0(增量 0)接收对象:100% (4350/4350),1.85 MiB |418.00 KiB/s,完成.解析增量:100% (2388/2388),完成.检查连通性...完成.

我们在/tmp/behave中有behave的setup.py:

(test_env) $ ls/tmp/behave/setup.py/tmp/behave/setup.py

从 git repo 安装 python 包

(test_env) $ cd/tmp/behave &&点安装.运行安装...安装/private/tmp/test_env/lib/python2.7/site-packages/enum34-1.0-py2.7.egg完成了行为==1.2.5a1的处理依赖

如果我们从 /tmp

运行上述解决方案<预><代码>>>>导入点>>>sorted(["%s==%s" % (i.key, i.version) for i in pip.get_installed_distributions()])['behave==1.2.5a1', 'enum34==1.0', 'parse-type==0.3.4', 'parse==1.6.4', 'six==1.6.1']>>>导入操作系统>>>os.getcwd()'/私人/tmp'

如果我们从 /tmp/behave 运行上述解决方案

<预><代码>>>>导入点>>>sorted(["%s==%s" % (i.key, i.version) for i in pip.get_installed_distributions()])['enum34==1.0', 'parse-type==0.3.4', 'parse==1.6.4', 'six==1.6.1']>>>导入操作系统>>>os.getcwd()'/私人/tmp/行为'

第二个示例中缺少

behave==1.2.5a1,因为工作目录包含 behavesetup.py 文件.

我在文档中找不到对此问题的任何参考.也许我会为它打开一个错误.

I would like to get a list of Python modules, which are in my Python installation (UNIX server).

How can you get a list of Python modules installed in your computer?

解决方案

Solution

Do not use with pip > 10.0!

My 50 cents for getting a pip freeze-like list from a Python script:

import pip
installed_packages = pip.get_installed_distributions()
installed_packages_list = sorted(["%s==%s" % (i.key, i.version)
     for i in installed_packages])
print(installed_packages_list)

As a (too long) one liner:

sorted(["%s==%s" % (i.key, i.version) for i in pip.get_installed_distributions()])

Giving:

['behave==1.2.4', 'enum34==1.0', 'flask==0.10.1', 'itsdangerous==0.24', 
 'jinja2==2.7.2', 'jsonschema==2.3.0', 'markupsafe==0.23', 'nose==1.3.3', 
 'parse-type==0.3.4', 'parse==1.6.4', 'prettytable==0.7.2', 'requests==2.3.0',
 'six==1.6.1', 'vioozer-metadata==0.1', 'vioozer-users-server==0.1', 
 'werkzeug==0.9.4']

Scope

This solution applies to the system scope or to a virtual environment scope, and covers packages installed by setuptools, pip and (god forbid) easy_install.

My use case

I added the result of this call to my flask server, so when I call it with http://example.com/exampleServer/environment I get the list of packages installed on the server's virtualenv. It makes debugging a whole lot easier.

Caveats

I have noticed a strange behaviour of this technique - when the Python interpreter is invoked in the same directory as a setup.py file, it does not list the package installed by setup.py.

Steps to reproduce:

Create a virtual environment

$ cd /tmp
$ virtualenv test_env
New python executable in test_env/bin/python
Installing setuptools, pip...done.
$ source test_env/bin/activate
(test_env) $ 

Clone a git repo with setup.py

(test_env) $ git clone https://github.com/behave/behave.git
Cloning into 'behave'...
remote: Reusing existing pack: 4350, done.
remote: Total 4350 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (4350/4350), 1.85 MiB | 418.00 KiB/s, done.
Resolving deltas: 100% (2388/2388), done.
Checking connectivity... done.

We have behave's setup.py in /tmp/behave:

(test_env) $ ls /tmp/behave/setup.py
/tmp/behave/setup.py

Install the python package from the git repo

(test_env) $ cd /tmp/behave && pip install . 
running install
...
Installed /private/tmp/test_env/lib/python2.7/site-packages/enum34-1.0-py2.7.egg
Finished processing dependencies for behave==1.2.5a1

If we run the aforementioned solution from /tmp

>>> import pip
>>> sorted(["%s==%s" % (i.key, i.version) for i in pip.get_installed_distributions()])
['behave==1.2.5a1', 'enum34==1.0', 'parse-type==0.3.4', 'parse==1.6.4', 'six==1.6.1']
>>> import os
>>> os.getcwd()
'/private/tmp'

If we run the aforementioned solution from /tmp/behave

>>> import pip
>>> sorted(["%s==%s" % (i.key, i.version) for i in pip.get_installed_distributions()])
['enum34==1.0', 'parse-type==0.3.4', 'parse==1.6.4', 'six==1.6.1']
>>> import os
>>> os.getcwd()
'/private/tmp/behave'

behave==1.2.5a1 is missing from the second example, because the working directory contains behave's setup.py file.

I could not find any reference to this issue in the documentation. Perhaps I shall open a bug for it.

这篇关于如何获取本地安装的 Python 模块的列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆