- How to List Python Packages – Globally Installed vs Locally Installed
- How to List Python Packages
- How to List Python Packages that are Globally Installed
- How to List Python Packages that are Locally Installed
- Pip
- Pipenv
- ActiveState Platform
- Conda
- Anaconda Navigator
- How to Determine the Location of Globally Installed Packages
- Using Pip Show for Package Location
- How to Determine the Location of Locally Installed Packages
- How to List Installed Python Packages
- List Installed Packages with Pip
- List Packages in a Console with Pip
- List Modules in a Console without Pip
- List Installed Packages with Pipenv
- List Installed Packages with Anaconda Navigator
- List Installed Packages with Conda
- Globally vs Locally Installed Packages
- List Installed Packages with the ActiveState Platform
- How do I check whether a module is installed in Python, and install it if needed?
- How do I check whether a module is installed in Python, and install it if needed?
How to List Python Packages – Globally Installed vs Locally Installed
When a package is installed globally, it’s made available to all users that log into the system. Typically, that means Python and all packages will get installed to a directory under /usr/local/bin/ for a Unix-based system, or \Program Files\ for Windows.
Conversely, when a package is installed locally, it’s only made available to the user that installed it. Locally installed Python and all packages will be installed under a directory similar to ~/.local/bin/ for a Unix-based system, or \Users\Username\AppData\Local\Programs\ for Windows.
How to List Python Packages
As a best practice, you should always install Python and the packages you need into a separate virtual environment for each project. This creates an isolated environment that will avoid many of the problems that arise because of shared libraries/ dependencies.
The Pip Package Manager can be used to list both globally and locally installed Python packages. The Pipenv, Anaconda Navigator and Conda package managers can also be used to list locally installed packages in their respective environments.
Before listing packages, it’s always a good practice to ensure that up-to-date versions of Python, Pip, Pipenv, Anaconda Navigator and Conda are in place.
How to List Python Packages that are Globally Installed
Pip installs packages globally by default. To list globally installed packages and their version # use:
To list a single globally installed package and its version #, use the following command depending on your OS:
How to List Python Packages that are Locally Installed
Pip
Although pip installs packages globally by default, packages that have been installed locally with the –user option can also be listed using the same –user option, as follows:
If you just want to list a single, locally installed package and its version #, you can use one of the following commands, depending on your OS:
Pipenv
To list locally installed packages and their version # within a pipenv environment, cd into a pipenv project and enter the following command:
This command will list all packages installed, including any dependencies that are found in a Pipfile.lock file.
ActiveState Platform
If you prefer to see a visual representation of the packages in your local/virtual environment, you can use the ActiveState Platform’s Web GUI, which shows:
- Installed top-level packages
- Installed package dependencies
- Installed shared (i.e., OS-level) dependencies
Conda
To list locally installed packages and their version # within a Conda environment, open the Anaconda Prompt and enter the following command:
Anaconda Navigator
To list all installed packages in a local Anaconda environment using Anaconda Navigator, do the following:
- Start the Anaconda Navigator application.
- Select Environments in the left-hand column.
- A dropdown box at the center-top of the GUI should list installed packages. If not, then select Installed from the dropdown menu to list the packages.
How to Determine the Location of Globally Installed Packages
As noted earlier, globally installed Python packages can typically be found in the default install location for your OS. However, it is possible to install packages into a non-default directory. In order to determine where global packages have been installed, use the following command:
To show the location of globally installed packages in a python console, use the following script:
>>> import site >>> print(site.getsitepackages())'
Using Pip Show for Package Location
The pip show command can provide information about a single, globally installed package, including its location:
How to Determine the Location of Locally Installed Packages
To list the location of locally installed packages, use the following command:
The ActiveState Platform automatically builds all Python packages including linked C libraries from source code, and packages them for Windows, Linux and macOS. Because it does it all server-side, there’s no need to maintain local build environments.
How to List Installed Python Packages
The Pip, Pipenv, Anaconda Navigator, and Conda Package Managers can all be used to list installed Python packages.
You can also use the ActiveState Platform’s command line interface (CLI), the State Tool to list all installed packages using a simple “state packages” command. For a complete list of all packages and dependencies (including OS-level and transitive dependencies, as well as shared libraries), you can use the Web GUI, which provides a full Bill of Materials view. Give it a try by signing up for a free ActiveState Platform account .
Before getting a list of installed packages, it’s always a good practice to ensure that up-to-date versions of Python, Pip, Anaconda Navigator and Conda are in place.
List Installed Packages with Pip
Both pip list and pip freeze will generate a list of installed packages, just with differently formatted results. Keep in mind that pip list will list ALL installed packages (regardless of how they were installed). while pip freeze will list only everything installed by Pip.
Package Version ---------------------------------- ---------- absl-py 0.7.0
List Packages in a Console with Pip
To list all installed packages from a Python console using pip, you can utilize the following script:
>>> 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)
['absl-py==0.7.0', 'adodbapi==2.6.0.7', 'alabaster==0.7.12', 'alembic==1.0.7', 'amqp==2.4.1', 'anyjson==0.3.3',
List Modules in a Console without Pip
To list all installed modules from a python console without pip, you can use the following command:
Note that there are some drawbacks to this approach, including:
- If there are a lot of installed packages, this method can take a long time to import each module before it can search that module’s path for sub-modules.
- Modules that have code outside of an if __name__ == “__main__”: code block, and if user input is expected, may cause the code to enter an infinite loop or hang.
List Installed Packages with Pipenv
The pipenv lock -r command can be used to generate output from a pipfile.lock file in a pipenv environment. All packages, including dependencies will be listed in the output. For example:
-i https://pypi.org/simple certifi==2019.11.28 chardet==3.0.4 idna==2.9 requests==2.23.0 urllib3==1.25.8
List Installed Packages with Anaconda Navigator
To list installed packages in an Anaconda environment using Anaconda Navigator, do the following:
- Start the Anaconda Navigator application.
- Select Environments in the left column.
- A dropdown box at the center-top of the GUI should list installed packages. If not, then select Installed in the dropdown menu to list all packages.
List Installed Packages with Conda
The conda list command can be used to list all packages in a conda environment:
# packages in environment at C:\Anaconda2_4.3.1: # _license 1.1 py27_1 alabaster 0.7.9 py27_0
Globally vs Locally Installed Packages
For information about generating a list of installed packages globally vs locally, refer to:
List Installed Packages with the ActiveState Platform
To view a list of installed Python packages in your currently active project using the ActiveState Platform, run the following command on the command line:
The output is a full list of installed packages in your current project:
matplotlib numpy pandas scikit-learn scipy
You can also obtain a complete software bill of materials view of all packages, dependencies, transitives dependencies (ie., dependencies of dependencies), OS-level dependencies and shared libraries (ie., OpenSSL) using the ActiveState Platform’s Web GUI:
The ActiveState Platform automatically builds all Python packages including linked C libraries from source code, and packages them for Windows, Linux and macOS. Because it does it all server-side, there’s no need to maintain local build environments.
How do I check whether a module is installed in Python, and install it if needed?
Warning: It is highly recommended to install python-modules using official Ubuntu repository only and not to use the pip method as superuser(i.e., as root or using sudo ). In some cases it may leave your system unusable by breaking system python.
Like in your answer math module exists and for numby the traceback didnt appear and for echo $? output was 0, Does it mean numpy is also present in my system?
In case you don’t have the module numpy how will you import it? When we do the coding we call the import statement by from numpy import *, will it install the module ? If not how will we install a new module?
It will be a septate question how to install a module. Need different packages for different module. For example to install vpython you need to install it as sudo apt-get install python-visual libgtkglextmm-x11-1.2-dev
don’t use sudo pip ; it may break system python. Use apt-get to install packages for system python. You could use pip —user option or virtualenv to install Python packages for yourself.
In case we do not want to unwantedly import a module in question (which would happen in a try statement) we can make use of sys.modules to test modules that are installed and were imported before.
In the python shell issue:
Then test for installed modules:
>>> 'numpy' in sys.modules True >>> 'scipy' in sys.modules False
Note that only those modules that were imported before give True on this test, all other modules (even if installed) result in False.
Another alternative to try an import statement in the python console is calling the inbuilt help() function. This will not give a documentation for non-installed modules, e.g.
>>> help('scipy') no Python documentation found for 'scipy'
The output of very long help documents of installed modules can be interrupted with Q .
Now to install missing modules it is recommended to use the Ubuntu package management (and not the Python pip way) because we need root access and also to prevent messing up our heavily Python-dependend system. For the module in question this would e.g. be:
sudo apt-get install python-scipy ## for Python2 sudo apt-get install python3-scipy ## for Python3
After installation we then can add them to the sys.modules dictionary by importing them once.
How do I check whether a module is installed in Python, and install it if needed?
Warning: It is highly recommended to install python-modules using official Ubuntu repository only and not to use the pip method as superuser(i.e., as root or using sudo ). In some cases it may leave your system unusable by breaking system python.
Like in your answer math module exists and for numby the traceback didnt appear and for echo $? output was 0, Does it mean numpy is also present in my system?
In case you don’t have the module numpy how will you import it? When we do the coding we call the import statement by from numpy import *, will it install the module ? If not how will we install a new module?
It will be a septate question how to install a module. Need different packages for different module. For example to install vpython you need to install it as sudo apt-get install python-visual libgtkglextmm-x11-1.2-dev
don’t use sudo pip ; it may break system python. Use apt-get to install packages for system python. You could use pip —user option or virtualenv to install Python packages for yourself.
In case we do not want to unwantedly import a module in question (which would happen in a try statement) we can make use of sys.modules to test modules that are installed and were imported before.
In the python shell issue:
Then test for installed modules:
>>> 'numpy' in sys.modules True >>> 'scipy' in sys.modules False
Note that only those modules that were imported before give True on this test, all other modules (even if installed) result in False.
Another alternative to try an import statement in the python console is calling the inbuilt help() function. This will not give a documentation for non-installed modules, e.g.
>>> help('scipy') no Python documentation found for 'scipy'
The output of very long help documents of installed modules can be interrupted with Q .
Now to install missing modules it is recommended to use the Ubuntu package management (and not the Python pip way) because we need root access and also to prevent messing up our heavily Python-dependend system. For the module in question this would e.g. be:
sudo apt-get install python-scipy ## for Python2 sudo apt-get install python3-scipy ## for Python3
After installation we then can add them to the sys.modules dictionary by importing them once.