How can I get a list of locally installed Python modules?

前端 未结 30 2161
庸人自扰
庸人自扰 2020-11-22 04:32

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

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

相关标签:
30条回答
  • 2020-11-22 05:00

    From the shell

    ls site-packages
    

    If that's not helpful, you can do this.

    import sys
    import os
    for p in sys.path:
        print os.listdir( p )
    

    And see what that produces.

    0 讨论(0)
  • 2020-11-22 05:01

    Installation

    pip install pkgutil
    

    Code

    import pkgutil
    
    for i in pkgutil.iter_modules(None): # returns a tuple (path, package_name, ispkg_flag)
        print(i[1]) #or you can append it to a list
    

    Sample Output:

    multiprocessing
    netrc
    nntplib
    ntpath
    nturl2path
    numbers
    opcode
    pickle
    pickletools
    pipes
    pkgutil
    
    0 讨论(0)
  • 2020-11-22 05:04

    In normal shell just use

    pydoc modules
    
    0 讨论(0)
  • 2020-11-22 05:04

    This will help

    In terminal or IPython, type:

    help('modules')
    

    then

    In [1]: import                      #import press-TAB
    Display all 631 possibilities? (y or n)
    ANSI                   audiodev               markupbase
    AptUrl                 audioop                markupsafe
    ArgImagePlugin         avahi                  marshal
    BaseHTTPServer         axi                    math
    Bastion                base64                 md5
    BdfFontFile            bdb                    mhlib
    BmpImagePlugin         binascii               mimetools
    BufrStubImagePlugin    binhex                 mimetypes
    CDDB                   bisect                 mimify
    CDROM                  bonobo                 mmap
    CGIHTTPServer          brlapi                 mmkeys
    Canvas                 bsddb                  modulefinder
    CommandNotFound        butterfly              multifile
    ConfigParser           bz2                    multiprocessing
    ContainerIO            cPickle                musicbrainz2
    Cookie                 cProfile               mutagen
    Crypto                 cStringIO              mutex
    CurImagePlugin         cairo                  mx
    DLFCN                  calendar               netrc
    DcxImagePlugin         cdrom                  new
    Dialog                 cgi                    nis
    DiscID                 cgitb                  nntplib
    DistUpgrade            checkbox               ntpath
    
    0 讨论(0)
  • 2020-11-22 05:07
    1. to get all available modules, run sys.modules
    2. to get all installed modules (read: installed by pip), you may look at pip.get_installed_distributions()

    For the second purpose, example code:

    import pip
    for package in pip.get_installed_distributions():
        name = package.project_name # SQLAlchemy, Django, Flask-OAuthlib
        key = package.key # sqlalchemy, django, flask-oauthlib
        module_name = package._get_metadata("top_level.txt") # sqlalchemy, django, flask_oauthlib
        location = package.location # virtualenv lib directory etc.
        version = package.version # version number
    
    0 讨论(0)
  • 2020-11-22 05:08

    Now, these methods I tried myself, and I got exactly what was advertised: All the modules.

    Alas, really you don't care much about the stdlib, you know what you get with a python install.

    Really, I want the stuff that I installed.

    What actually, surprisingly, worked just fine was:

    pip freeze
    

    Which returned:

    Fabric==0.9.3
    apache-libcloud==0.4.0
    bzr==2.3b4
    distribute==0.6.14
    docutils==0.7
    greenlet==0.3.1
    ipython==0.10.1
    iterpipes==0.4
    libxml2-python==2.6.21
    

    I say "surprisingly" because the package install tool is the exact place one would expect to find this functionality, although not under the name 'freeze' but python packaging is so weird, that I am flabbergasted that this tool makes sense. Pip 0.8.2, Python 2.7.

    0 讨论(0)
提交回复
热议问题