列出已安装的 python 站点包?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2300794/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-04 00:18:35  来源:igfitidea点击:

Listing installed python site-packages?

pythondjango

提问by panchicore

from distutils.sysconfig import get_python_lib; print get_python_lib()

Returns: /usr/lib/python2.6/site-packages

返回: /usr/lib/python2.6/site-packages

import sys; print sys.path

Returns: ['', '/usr/lib/python2.6/site-packages/Django-1.1.1-py2.6.egg', '/usr/lib/python2.6/site-packages/pip-0.6.3-py2.6.egg', '/usr/lib/python2.6/site-packages/TRML2PDF-1.0-py2.6.egg', '/usr/lib/python2.6/site-packages/django_threaded_multihost-1.3_3-py2.6.egg',...............

返回: ['', '/usr/lib/python2.6/site-packages/Django-1.1.1-py2.6.egg', '/usr/lib/python2.6/site-packages/pip-0.6.3-py2.6.egg', '/usr/lib/python2.6/site-packages/TRML2PDF-1.0-py2.6.egg', '/usr/lib/python2.6/site-packages/django_threaded_multihost-1.3_3-py2.6.egg',...............

But how to list the "importable name" from the site-packages installed? E.g: (before import results) django, pip, trm2pdf....

但是如何从安装的站点包中列出“可导入名称”?例如:(在导入结果之前)django, pip, trm2pdf....

Thanks.

谢谢。

采纳答案by miku

Check out yolk.

检查蛋黄

Yolk is a Python command-line tool and library for obtaining information about packages installed by setuptools, easy_install and distutils (Python 2.5) and for querying PyPI (Python Package Index a.k.a. The Cheese Shop).

Yolk 是一个 Python 命令行工具和库,用于获取有关 setuptools、easy_install 和 distutils(Python 2.5)安装的包的信息以及查询 PyPI(Python 包索引,又名 The Cheese Shop)。

回答by Vajk Hermecz

pipis the one for the job, as it is a tool for installing and managing Python packages. After install you have to execute:

pip很适合这项工作,因为它是一种用于安装和管理 Python 包的工具。安装后你必须执行:

pip freeze

That will output the packages and version information in pips requirements format (can be used later to install those packages with one command). The format of the output is like:

这将以 pips 要求格式输出包和版本信息(稍后可以使用一个命令安装这些包)。输出的格式如下:

querystring-parser==1.0
raven==1.4.6
requests==0.14.2
scipy==0.10.1

回答by Peter Rowell

You want sys.modules.

你要sys.modules

import pprint, sys
pprint.pprint(sys.modules)

You can slice and dice from there.

你可以从那里切片和切块。

回答by unutbu

You could use pkgutil. This lists all modules inside /usr/lib/python2.6/site-packages: (Unlike sys.modules, this lists modules without you having to import them first).

您可以使用pkgutil。这列出了 /usr/lib/python2.6/site-packages 中的所有模块:(与 不同sys.modules,这列出了模块而无需先导入它们)。

import pkgutil
print [name for module_loader,name,ispkg in
          pkgutil.walk_packages(['/usr/lib/python2.6/site-packages'])]

Edit: The docsdo not list walk_packages. However, pkgutilincludes walk_packagesin pkgutil.__all__. This means it is part of pkgutil's public interface. You can find the following documentation on walk_packagesin /usr/lib/python2.6/pkgutil.py or by typing help(pkgutil.walk_packages):

编辑文档没有列出walk_packages。但是,pkgutil包含walk_packagespkgutil.__all__. 这意味着它是 pkgutil 公共接口的一部分。您可以walk_packages在 /usr/lib/python2.6/pkgutil.py 或通过键入以下文件找到以下文档help(pkgutil.walk_packages)

Definition: pkgutil.walk_packages(path=None, prefix='', onerror=None)
Docstring:
    Yields (module_loader, name, ispkg) for all modules recursively
    on path, or, if path is None, all accessible modules.

    'path' should be either None or a list of paths to look for
    modules in.

    'prefix' is a string to output on the front of every module name
    on output.

    Note that this function must import all *packages* (NOT all
    modules!) on the given path, in order to access the __path__
    attribute to find submodules.

    'onerror' is a function which gets called with one argument (the
    name of the package which was being imported) if any exception
    occurs while trying to import a package.  If no onerror function is
    supplied, ImportErrors are caught and ignored, while all other
    exceptions are propagated, terminating the search.

    Examples:

    # list all modules python can access
    walk_packages()

    # list all submodules of ctypes
    walk_packages(ctypes.__path__, ctypes.__name__+'.')

回答by éric Araujo

I don't know of an easy way. A Python distribution (i.e. something that was installed, like Django 1.3) can have zero or more Python modules, zero or more Python packages (i.e. modules that have submodules, not what other tools call packages), zero or more scripts, zero or more data files. If you installed with pip or easy_install, metadata files are written in the egg-info directories/files/zipped directories, so a tool could walk these files to display what modules or packages were installed for a distribution, but I don't know any tool that does that.

我不知道有什么简单的方法。Python 发行版(即已安装的东西,如 Django 1.3)可以有零个或多个 Python 模块、零个或多个 Python 包(即具有子模块的模块,而不是其他工具调用的包)、零个或多个脚本、零个或多个数据文件。如果您使用 pip 或 easy_install 安装,元数据文件将写入 egg-info 目录/files/zipped 目录中,因此工具可以遍历这些文件以显示为分发安装了哪些模块或包,但我不知道任何这样做的工具。

yolk and pip freeze will only list distributions (even if they call them packages), to let you know what version are installed, and then you can upgrade or uninstall them.

yolk 和 pip freeze 只会列出发行版(即使他们称它们为包),让您知道安装了哪个版本,然后您可以升级或卸载它们。

Inspecting sys.modules only gives info about modules imported during the current Python session.

检查 sys.modules 仅提供有关在当前 Python 会话期间导入的模块的信息。

So to know what modules are importable on your system after installing distributions, you have to resort to crude inspection of site-packages and similar directories, or write some code to walk over packaging metadata files and extract modules. This won't work for distributions installed with pure distutils.

因此,要知道在安装发行版后哪些模块可以导入到您的系统中,您必须对站点包和类似目录进行粗略检查,或者编写一些代码来遍历打包元数据文件并提取模块。这不适用于使用纯 distutils 安装的发行版。

This is clearly imperfect and confusing; we're still working on Python packaging.

这显然是不完美和令人困惑的;我们仍在研究 Python 打包。

BTW, can I ask what is the use case for your question? Typically you install one distribution to do something with it, and the same documentation that tells you what to install will tell you what to import.

顺便说一句,我能问一下你的问题的用例是什么吗?通常,您安装一个发行版以对其进行处理,并且告诉您要安装什么的相同文档将告诉您要导入什么。