Skip to content
Fix Code Error

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

March 13, 2021 by Code Error
Posted By: Anonymous

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?

Solution

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.

Answered By: Anonymous

Related Articles

  • Problems Installing CRA & NextJS from NPM…
  • Flask and Jinja2 - Filter is…
  • Is CSS Turing complete?
  • TypeScript metadata reflection references other…
  • How can I combine Vue.js with Flask?
  • Solve Cross Origin Resource Sharing with Flask
  • Flask ImportError: No Module Named Flask
  • How to generate the JSONSchema for the JAXB/Moxy…
  • Use secret key to secure flask API - python
  • Get the data received in a Flask request
  • Python is not calling fucntions properly
  • How to excecute code after Flask `app.run()`…
  • Flask raises TemplateNotFound error even though…
  • Rails wrong number of arguments error when…
  • Flask Value error view function did not return a response
  • How to host a flask app on a subfolder of a website?
  • center 3 items on 2 lines
  • What is the best way to pass flask variables…
  • Weaseyprint, Cairo, Dajngo on Pythonanywhere 25MAY21…
  • Can I serve multiple clients using just Flask…
  • Freezing Row 1 and Column A at the same time
  • "OSError: [Errno 1] Operation not permitted" when…
  • How can I represent an 'Enum' in Python?
  • vue front-end flask back-end integration
  • How to serve static files in Flask
  • Find which version of package is installed with pip
  • Error message "Forbidden You don't have permission…
  • long long int vs. long int vs. int64_t in C++
  • How do I get Flask to run on port 80?
  • Vue js vuecli3 application does not work in ie11…
  • Install pip in docker
  • Pivoting a defined number of rows into columns…
  • Change the value of a Clojure var without touching…
  • How to resolve versionConflict error in Flask (PyJWT…
  • Is it possible to run custom code before Swagger…
  • Can't correctly import external JS into Aurelia…
  • How can I find the product GUID of an installed MSI setup?
  • In Flask, What is request.args and how is it used?
  • How to deploy a Flask / Vue.js web application
  • VUE Error when run test unit
  • Interrupt an earlier timeout event in Simpy
  • Saving every nth packet from a UDP stream
  • Object is possibly undefined -- but it's not...?
  • How to send file metadata over api using fetch
  • using d3.js with aurelia framework
  • Azure Availability Zone ARM Config
  • Rename computer and join to domain in one step with…
  • How can I pass data from Flask to JavaScript in a template?
  • SystemJS (Aurelia with jspm) fails to load…
  • Running Python file from C# Windows Form
  • Can't install Scipy through pip
  • pip cannot install anything
  • How to convert number to words in java
  • How do I connect to this localhost from another…
  • Remove similar tuple from dictionary of tuples
  • Aurelia bundling issue with virtual directory
  • Auto reloading python Flask app upon code changes
  • What is the difference between "long", "long long",…
  • How to install pip with Python 3?
  • Enable CORS in a Backbone client with a Flask server
  • aurelia-bootstrapper not found after upgrading to jspm beta
  • return two dimensional array from function with…
  • Getting an object array from an Angular service
  • Pip not working in base conda environment
  • python object() takes no parameters error
  • How to update Python?
  • getaddrinfo: nodename nor servname provided, or not known
  • GUI-based or Web-based JSON editor that works like…
  • Python File Error: unpack requires a buffer of 16 bytes
  • Redirecting to URL in Flask
  • How can I install pip on Windows?
  • OpenCL - Approximation of Pi via Monte Carlo…
  • implement flask-healthz for python3
  • How do I count unique visitors to my site?
  • Cannot import keras after installation
  • What should be the values of GOPATH and GOROOT?
  • Render html table with underscore template engine…
  • Dealing with multiple Python versions and PIP?
  • How to use requirements.txt to install all…
  • How to get POSTed JSON in Flask?
  • Remote end closed connection without response
  • Pip freeze vs. pip list
  • Heroku "Missing required flag -a --app" error after…
  • Send data from a textbox into Flask?
  • Pip - Fatal error in launcher: Unable to create…
  • I wrote a simple python game but functions in that…
  • Smart way to truncate long strings
  • Download and save PDF file with Python requests module
  • How do I set response headers in Flask?
  • How to use html template with vue.js
  • python 3.2 UnicodeEncodeError: 'charmap' codec can't…
  • Can't install via pip because of egg_info error
  • Configure Flask dev server to be visible across the network
  • How to Install pip for python 3.7 on Ubuntu 18?
  • How do I get to the Clojure metadata when the var is…
  • Error installing PyPI xmlsec==1.3.3 Python2
  • Does uninstalling a package with "pip" also remove…
  • How do SO_REUSEADDR and SO_REUSEPORT differ?
  • How to create a dynamic form vuejs vuetify?
  • Conda update failed: SSL error: [SSL:…

Disclaimer: This content is shared under creative common license cc-by-sa 3.0. It is generated from StackExchange Website Network.

Post navigation

Previous Post:

Font scaling based on width of container

Next Post:

How do you assert that a certain exception is thrown in JUnit 4 tests?

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

.net ajax android angular arrays aurelia backbone.js bash c++ css dataframe ember-data ember.js excel git html ios java javascript jquery json laravel linux list mysql next.js node.js pandas php polymer polymer-1.0 python python-3.x r reactjs regex sql sql-server string svelte typescript vue-component vue.js vuejs2 vuetify.js

  • you shouldn’t need to use z-index
  • No column in target database, but getting “The schema update is terminating because data loss might occur”
  • Angular – expected call-signature: ‘changePassword’ to have a typedeftslint(typedef)
  • trying to implement NativeAdFactory imports deprecated method by default in flutter java project
  • What should I use to get an attribute out of my foreign table in Laravel?
© 2022 Fix Code Error