开发者

Listing installed python site-packages? [duplicate]

开发者 https://www.devze.com 2022-12-20 14:30 出处:网络
This question already has answers here: How do I get a list of locally installed Python modules? (33 answers)
This question already has answers here: How do I get a list of locally installed Python modules? (33 answers) Closed 9 years ago.
from distutils.sysconfig import get_python_lib; print get_python_lib()

Returns: /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/pyt开发者_开发百科hon2.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....

Thanks.


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

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:

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


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).


You want sys.modules.

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

You can slice and dice from there.


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).

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

Edit: The docs do not list walk_packages. However, pkgutil includes walk_packages in pkgutil.__all__. This means it is part of pkgutil's public interface. You can find the following documentation on walk_packages in /usr/lib/python2.6/pkgutil.py or by typing 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__+'.')


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.

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.

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

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.

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

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消