Friday, November 20, 2020

Where are my Python packages?

Sometimes when I use a Python package, I run into some problems. I want to take a look at the source code of the package. But where are the packages?

There are a couple of methods. First, we can view where Python is looking for packages. For globally installed packages, we can locate the paths as follows,


$ python
Python 3.7.4 (default, Aug  9 2019, 18:34:13) [MSC v.1915 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> for p in sys.path:
...   print(p)
...

The global site packages are at,


python
Python 3.7.4 (default, Aug  9 2019, 18:34:13) [MSC v.1915 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import site
>>> for p in site.getsitepackages():
...   print(p)
...

If we have pip installed and know the package name, we can use a command like


$ pip show numpy

What if we want to know the path to the locally installed pacakges (user-installed packages)?


$ python
Python 3.7.4 (default, Aug  9 2019, 18:34:13) [MSC v.1915 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import site
>>> site.getusersitepackages()
...