代码之家  ›  专栏  ›  技术社区  ›  panchicore

是否列出已安装的python站点包?[副本]

  •  11
  • panchicore  · 技术社区  · 15 年前
    from distutils.sysconfig import get_python_lib; print get_python_lib()
    

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

    import sys; print sys.path
    

    返回: ['', '/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',...............

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

    谢谢。

    5 回复  |  直到 13 年前
        1
  •  7
  •   miku    15 年前
        2
  •  11
  •   Vajk Hermecz    13 年前

    pip

    pip freeze
    

    querystring-parser==1.0
    raven==1.4.6
    requests==0.14.2
    scipy==0.10.1
    
        3
  •  4
  •   Peter Rowell    15 年前

    sys.modules

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

        4
  •  2
  •   unutbu    15 年前

    pkgutil sys.modules

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

    The docs walk_packages pkgutil pkgutil.__all__ 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__+'.')
    
        5
  •  0
  •   merwok    13 年前