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

前端 未结 30 2209
庸人自扰
庸人自扰 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 04:49

    Solution

    Do not use with pip > 10.0!

    My 50 cents for getting a pip freeze-like list from a Python script:

    import pip
    installed_packages = pip.get_installed_distributions()
    installed_packages_list = sorted(["%s==%s" % (i.key, i.version)
         for i in installed_packages])
    print(installed_packages_list)
    

    As a (too long) one liner:

    sorted(["%s==%s" % (i.key, i.version) for i in pip.get_installed_distributions()])
    

    Giving:

    ['behave==1.2.4', 'enum34==1.0', 'flask==0.10.1', 'itsdangerous==0.24', 
     'jinja2==2.7.2', 'jsonschema==2.3.0', 'markupsafe==0.23', 'nose==1.3.3', 
     'parse-type==0.3.4', 'parse==1.6.4', 'prettytable==0.7.2', 'requests==2.3.0',
     'six==1.6.1', 'vioozer-metadata==0.1', 'vioozer-users-server==0.1', 
     'werkzeug==0.9.4']
    

    Scope

    This solution applies to the system scope or to a virtual environment scope, and covers packages installed by setuptools, pip and (god forbid) easy_install.

    My use case

    I added the result of this call to my flask server, so when I call it with http://example.com/exampleServer/environment I get the list of packages installed on the server's virtualenv. It makes debugging a whole lot easier.

    Caveats

    I have noticed a strange behaviour of this technique - when the Python interpreter is invoked in the same directory as a setup.py file, it does not list the package installed by setup.py.

    Steps to reproduce:

    Create a virtual environment
    $ cd /tmp
    $ virtualenv test_env
    New python executable in test_env/bin/python
    Installing setuptools, pip...done.
    $ source test_env/bin/activate
    (test_env) $ 
    
    Clone a git repo with setup.py
    (test_env) $ git clone https://github.com/behave/behave.git
    Cloning into 'behave'...
    remote: Reusing existing pack: 4350, done.
    remote: Total 4350 (delta 0), reused 0 (delta 0)
    Receiving objects: 100% (4350/4350), 1.85 MiB | 418.00 KiB/s, done.
    Resolving deltas: 100% (2388/2388), done.
    Checking connectivity... done.
    

    We have behave's setup.py in /tmp/behave:

    (test_env) $ ls /tmp/behave/setup.py
    /tmp/behave/setup.py
    
    Install the python package from the git repo
    (test_env) $ cd /tmp/behave && pip install . 
    running install
    ...
    Installed /private/tmp/test_env/lib/python2.7/site-packages/enum34-1.0-py2.7.egg
    Finished processing dependencies for behave==1.2.5a1
    

    If we run the aforementioned solution from /tmp

    >>> import pip
    >>> sorted(["%s==%s" % (i.key, i.version) for i in pip.get_installed_distributions()])
    ['behave==1.2.5a1', 'enum34==1.0', 'parse-type==0.3.4', 'parse==1.6.4', 'six==1.6.1']
    >>> import os
    >>> os.getcwd()
    '/private/tmp'
    

    If we run the aforementioned solution from /tmp/behave

    >>> import pip
    >>> sorted(["%s==%s" % (i.key, i.version) for i in pip.get_installed_distributions()])
    ['enum34==1.0', 'parse-type==0.3.4', 'parse==1.6.4', 'six==1.6.1']
    >>> import os
    >>> os.getcwd()
    '/private/tmp/behave'
    

    behave==1.2.5a1 is missing from the second example, because the working directory contains behave's setup.py file.

    I could not find any reference to this issue in the documentation. Perhaps I shall open a bug for it.

    0 讨论(0)
  • 2020-11-22 04:49

    For anyone wondering how to call pip list from a Python program you can use the following:

    import pip
    pip.main(['list])  # this will print all the packages
    
    0 讨论(0)
  • 2020-11-22 04:50

    I normally use pip list to get a list of packages (with version).

    This works in a virtual environment too, of course. To show what's installed in only the virtual environment (not global packages), use pip list --local.

    Here's documentation showing all the available pip list options, with several good examples.

    0 讨论(0)
  • 2020-11-22 04:52

    As of pip 10, the accepted answer will no longer work. The development team has removed access to the get_installed_distributions routine. There is an alternate function in the setuptools for doing the same thing. Here is an alternate version that works with pip 10:

    import pkg_resources
    installed_packages = pkg_resources.working_set
    installed_packages_list = sorted(["%s==%s" % (i.key, i.version)
         for i in installed_packages])
    print(installed_packages_list)
    

    Please let me know if it will or won't work in previous versions of pip, too.

    0 讨论(0)
  • 2020-11-22 04:52

    Aside from using pip freeze I have been installing yolk in my virtual environments.

    0 讨论(0)
  • 2020-11-22 04:53

    If we need to list the installed packages in the Python shell, we can use the help command as follows

    >>> help('modules package')
    
    0 讨论(0)
提交回复
热议问题