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

前端 未结 12 2019
灰色年华
灰色年华 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

提交回复
热议问题