By reading this tutorial, you will learn where the Python packages and modules are installed in your system and other Linux distributions. But the most important teaching in this tutorial is in the last section, where you will learn how to find the Python packages without memorizing the directories independently of the Linux distribution or installation method.
All examples shown in this article include screenshots, making it easy for every Linux user to understand them independently of their knowledge level.
Previous Necessary Step: Learning the Installed Python Version
To follow all instructions described in this article, you must first know the Python version installed in your system.
To learn it, you can use the ls command followed by the executable files path and replace the version with a wildcard as shown in the following:
As you can see, there are two Python versions currently installed in my system: Python 3.9 and Python 2.7. The other paths belong to the symbolic links.
Where Are Python Packages Stored When Installed Without Packages Manager
If the Python installation was done from sources or from Python installation mechanisms (like easy_install or Python setup.py) and not from a packages manager like apt-get or aptitude among others, Python packages are stored under the /usr/local/lib/python<version>/ directory.
This directory can be defined as universal and valid for almost every Linux distribution because it’s based on installation methods available for different distributions independently of their package managers. Of course, this is not valid when Python is installed using a specific distro packages manager.
If you compiled Python from sources or installed it using the setup.py or easy_install, you can check this location using the ls command as shown in the following screenshot where 3.10 must be replaced with your actual Python version.
Where Are Python Packages When Installed Through Pip
Python packages installed using the pip command are stored under the /usr/local/lib/<version>/dist-packages/pip/ directory.
You can find the correct location by using the pip command followed by the list option as shown in the following:
Where Are Python Packages Installed in Debian/Ubuntu Distributions
If you are a Debian, Ubuntu, or other based distribution and you installed Python through the dpkg packages manager or one of its frontends like apt-get, apt or aptitude, the packages are stored in the /usr/lib/python<version> directory, as shown in the following image where 3.9 must be replaced with your actual Python version.
Note: As mentioned previously, if you don’t install Python using the dpkg, apt, apt-get or aptitude, the packages will have a different location described in the first section of this document.
Where Are Python Packages Installed in Red Hat Based Distributions & SUSE
Like in Debian/Ubuntu, Python packages without defined specific architecture are installed under the /usr/lib/python<version> directory.
But specific architecture Python packages in Red Hat are stored under a descriptive directory like /usr/lib64/python<version>.
Local Python packages are installed under /home/<user>/.local/lib/python<version>/.
How to Find Installed Python Packages Independently of the Installation Method
Whatever is your Linux distribution, you can always use the find command to search the files by type, in this case to find the Python packages using its .py extension as shown in the following figure where /usr is the parent directory in the recursive search, -type f defines that you are searching files and not directories, and “*.py” is the extension of files that you are looking for.
Finding Python Modules
To find the Python modules, open the Python console by executing the python<version>, where <version> must be replaced with your actual Python version. Then, run the command help(“modules”) as shown in the following example:
In our case, using the Python 3, we execute the following code:
Then, we also execute this following code:
As you can see, Python packages are installed in different locations depending on the Linux distribution, installation methods, and in some cases, depending on the architecture. But the find command is useful to search all Python packages independently of the installation method, distribution, or architecture, and is a valid technique for every Linux distribution. Learning the program versions or how to find the files by type or extension is extremely easy and mandatory for all the Linux users independently of their knowledge level. It is important to clarify that while this article provides examples including Python 2, this version was discontinued and Python 3 is the current version, with many improvements. Always try to install Python using your Linux distribution packages manager, automatically resolving the dependencies and easing the removal process before problems.
Thank you for reading this article. We hope it was useful. Keep following Linux Hint for more professional Linux tips and tutorials.