How to list all installed packages and their versions in Python?

前端 未结 12 1967
灰色年华
灰色年华 2020-12-12 10:07

Is there a way in Python to list all installed packages and their versions?

I know I can go inside python/Lib/site-packages and see what files and direc

相关标签:
12条回答
  • 2020-12-12 10:33

    yes! you should be using pip as your python package manager ( http://pypi.python.org/pypi/pip )

    with pip installed packages, you can do a

    pip freeze
    

    and it will list all installed packages. You should probably also be using virtualenv and virtualenvwrapper. When you start a new project, you can do

    mkvirtualenv my_new_project
    

    and then (inside that virtualenv), do

    pip install all_your_stuff
    

    This way, you can workon my_new_project and then pip freeze to see which packages are installed for that virtualenv/project.

    for example:

    ➜  ~  mkvirtualenv yo_dude
    New python executable in yo_dude/bin/python
    Installing setuptools............done.
    Installing pip...............done.
    virtualenvwrapper.user_scripts creating /Users/aaylward/dev/virtualenvs/yo_dude/bin/predeactivate
    virtualenvwrapper.user_scripts creating /Users/aaylward/dev/virtualenvs/yo_dude/bin/postdeactivate
    virtualenvwrapper.user_scripts creating /Users/aaylward/dev/virtualenvs/yo_dude/bin/preactivate
    virtualenvwrapper.user_scripts creating /Users/aaylward/dev/virtualenvs/yo_dude/bin/postactivate
    virtualenvwrapper.user_scripts creating /Users/aaylward/dev/virtualenvs/yo_dude/bin/get_env_details
    
    (yo_dude)➜  ~  pip install django
    Downloading/unpacking django
      Downloading Django-1.4.1.tar.gz (7.7Mb): 7.7Mb downloaded
      Running setup.py egg_info for package django
    
    Installing collected packages: django
      Running setup.py install for django
        changing mode of build/scripts-2.7/django-admin.py from 644 to 755
    
        changing mode of /Users/aaylward/dev/virtualenvs/yo_dude/bin/django-admin.py to 755
    Successfully installed django
    Cleaning up...
    
    (yo_dude)➜  ~  pip freeze
    Django==1.4.1
    wsgiref==0.1.2
    
    (yo_dude)➜  ~  
    

    or if you have a python package with a requirements.pip file,

    mkvirtualenv my_awesome_project
    pip install -r requirements.pip
    pip freeze
    

    will do the trick

    0 讨论(0)
  • 2020-12-12 10:35

    To run this in later versions of pip (tested on pip==10.0.1) use the following:

    from pip._internal.operations.freeze import freeze
    for requirement in freeze(local_only=True):
        print(requirement)
    
    0 讨论(0)
  • 2020-12-12 10:37

    You can try : Yolk

    For install yolk, try:

    easy_install yolk
    

    Yolk is a Python tool for obtaining information about installed Python packages and querying packages avilable on PyPI (Python Package Index).

    You can see which packages are active, non-active or in development mode and show you which have newer versions available by querying PyPI.

    0 讨论(0)
  • 2020-12-12 10:40

    If you want to get information about your installed python distributions and don't want to use your cmd console or terminal for it, but rather through python code, you can use the following code (tested with python 3.4):

    import pip #needed to use the pip functions
    for i in pip.get_installed_distributions(local_only=True):
        print(i)
    

    The pip.get_installed_distributions(local_only=True) function-call returns an iterable and because of the for-loop and the print function the elements contained in the iterable are printed out separated by new line characters (\n). The result will (depending on your installed distributions) look something like this:

    cycler 0.9.0
    decorator 4.0.4
    ipykernel 4.1.0
    ipython 4.0.0
    ipython-genutils 0.1.0
    ipywidgets 4.0.3
    Jinja2 2.8
    jsonschema 2.5.1
    jupyter 1.0.0
    jupyter-client 4.1.1
    #... and so on...
    
    0 讨论(0)
  • 2020-12-12 10:41

    If this is needed to run from within python you can just invoke subprocess

    from subprocess import PIPE, Popen
    
    pip_process = Popen(["pip freeze"], stdout=PIPE,
                       stderr=PIPE, shell=True)
    stdout, stderr = pip_process.communicate()
    print(stdout.decode("utf-8"))
    
    0 讨论(0)
  • 2020-12-12 10:42

    help('modules') should do it for you.

    in IPython :

    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)
提交回复
热议问题