tags:

views:

200

answers:

3
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/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....

Thanks.

+4  A: 

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

The MYYN
Nice!, If I run yolk -l ... It lists me: Django...etc,etc. but the correct "importable name" is django (lowercase). what about that?
panchicore
+2  A: 

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__+'.')
unutbu
+2  A: 

You want sys.modules.

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

You can slice and dice from there.

Peter Rowell
>>> import pprint, sys>>> pprint(sys.modules)Traceback (most recent call last): File "<stdin>", line 1, in <module>TypeError: 'module' object is not callable
panchicore
Try `pprint.pprint(sys.modules)`
Ned Deily
the output is: <code>{'UserDict': <module 'UserDict' from '/usr/lib/python2.6/UserDict.pyc'>,....</code> doesnt seem to be the installed pluggins.
panchicore
I can't speak for 2.6 as I'm running 2.5.4 across all of my machines.
Peter Rowell