To see where pip installs packages on your system, run the following command:
pip show <package_name>
And replace <package_name> with the actual name of the package.
Example: NumPy Location
For example, let’s see where NumPy is installed:
pip show numpy
Name: numpy Version: 1.22.2 Summary: NumPy is the fundamental package for array computing with Python. Home-page: https://www.numpy.org Author: Travis E. Oliphant et al. Author-email: None License: BSD Location: /usr/local/lib/python3.8/site-packages Requires: Required-by: torchvision, perfplot, opencv-python, matplotx, DALL-E, benchit
Here you can see that the location field says the package is installed at /usr/local/lib/python3.8/site-packages.
The location obviously depends on your system and Python version.
How to View All pip Package Locations
To list all the installed package locations, run the following command:
pip list -v
This spits out a huge list of different packages and their locations:
alabaster 0.7.8 /usr/lib/python3/dist-packages apparmor 2.13.3 /usr/lib/python3/dist-packages appdirs 1.4.3 /usr/lib/python3/dist-packages apturl 0.5.2 /usr/lib/python3/dist-packages ...
Now you understand how to check the pip package locations using the command line/terminal.
Next, let’s take a look at how you can find this information using a Python script.
How to View pip Package Location in Python Script?
In addition to using the command line to figure out the location of packages installed via pip, you can run a Python script to get the information.
There are two ways to do this:
- Use the site module.
- Use the help() function.
The site Module
To find the general location of pip packages in a Python script:
- Import the site package.
- Call the getsitepackages() function of the module.
- See a list of global package locations.
Here is how it looks in code:
>>> import site >>> site.getsitepackages() ['/usr/local/lib/python3.8/dist-packages', '/usr/lib/python3/dist-packages', '/usr/lib/python3.8/dist-packages']
And to get a user-specific package location as a string, call the getusersitepackages() function instead.
The help() Function
Of course, you can always use the help() function to get all kinds of information about any Python package or object.
This also shows you the location where the package is installed.
For example, lets’ see where the pandas package is installed:
>>> import pandas >>> help(pandas)
Running this piece of code opens up the package-specific manual.
If you scroll all the way down to the end of this output, you can see the FILE section where it shows you the path of the package.
By the way, in case you happen to be unfamiliar with the help() function in Python, I highly recommend you read this article. Using help() can help you a lot and save valuable coding time!
Today you learned how to check where pip installs packages on your system.
To recap, all you need to do is:
- Open up command line.
- Run pip show <package_name>.
Thanks for reading.