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

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

问题描述

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

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

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

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

推荐答案

解决方案

请勿与pip> 10.0一起使用!

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

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)

(太长)一个班轮:

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', 
 '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']

范围

此解决方案适用于系统范围或虚拟环境范围,并涵盖由setuptoolspip和(我将此调用的结果添加到了我的Flask服务器中,因此,当我使用http://example.com/exampleServer/environment调用它时,会获得服务器的virtualenv上安装的软件包的列表.它使调试变得非常容易.

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.

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

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.

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

使用setup.py克隆git repo

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.

我们在/tmp/behave中表现为setup.py:

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

从git repo安装python包

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

如果我们从/tmp

运行上述解决方案

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'

如果我们从/tmp/behave

运行上述解决方案

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,因为工作目录包含behavesetup.py文件.

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天全站免登陆