How do I find the location of my Python site-packages directory?
There are two types of site-packages directories, global and per user.
Global site-packages ("dist-packages") directories are listed in
sys.path
when you run:python -m site
For a more concise list run
getsitepackages
from the site module in Python code:python -c 'import site; print(site.getsitepackages())'
Caution: In virtual environments getsitepackages is not available with older versions of
virtualenv
,sys.path
from above will list the virtualenv's site-packages directory correctly, though. In Python 3, you may use the sysconfig module instead:python3 -c 'import sysconfig; print(sysconfig.get_paths()["purelib"])'
The per user site-packages directory (PEP 370) is where Python installs your local packages:
python -m site --user-site
If this points to a non-existing directory check the exit status of Python and see
python -m site --help
for explanations.Hint: Running
pip list --user
orpip freeze --user
gives you a list of all installed per user site-packages.
Practical Tips
<package>.__path__
lets you identify the location(s) of a specific package: (details)$ python -c "import setuptools as _; print(_.__path__)"
['/usr/lib/python2.7/dist-packages/setuptools']<module>.__file__
lets you identify the location of a specific module: (difference)$ python3 -c "import os as _; print(_.__file__)"
/usr/lib/python3.6/os.pyRun
pip show <package>
to show Debian-style package information:$ pip show pytest
Name: pytest
Version: 3.8.2
Summary: pytest: simple powerful testing with Python
Home-page: https://docs.pytest.org/en/latest/
Author: Holger Krekel, Bruno Oliveira, Ronny Pfannschmidt, Floris Bruynooghe, Brianna Laugher, Florian Bruhin and others
Author-email: None
License: MIT license
Location: /home/peter/.local/lib/python3.4/site-packages
Requires: more-itertools, atomicwrites, setuptools, attrs, pathlib2, six, py, pluggy
What is python's site-packages directory?
site-packages
is the target directory of manually built Python packages. When you build and install Python packages from source (using distutils, probably by executing python setup.py install
), you will find the installed modules in site-packages
by default.
There are standard locations:
- Unix (pure)1:
prefix/lib/pythonX.Y/site-packages
- Unix (non-pure):
exec-prefix/lib/pythonX.Y/site-packages
- Windows:
prefix\Lib\site-packages
1 Pure means that the module uses only Python code. Non-pure can contain C/C++ code as well.
site-packages
is by default part of the Python search path, so modules installed there can be imported easily afterwards.
Useful reading
- Installing Python Modules (for Python 2)
- Installing Python Modules (for Python 3)
Where are the python modules stored?
- Is there a way to obtain a list of
Python modules available (i.e.
installed) on a machine?
This works for me:
help('modules')
- Where is the module code actually
stored on my machine?
Usually in /lib/site-packages
in your Python folder. (At least, on Windows.)
You can use sys.path
to find out what directories are searched for modules.
How to retrieve a module's path?
import a_module
print(a_module.__file__)
Will actually give you the path to the .pyc file that was loaded, at least on Mac OS X. So I guess you can do:
import os
path = os.path.abspath(a_module.__file__)
You can also try:
path = os.path.dirname(a_module.__file__)
To get the module's directory.
How do I find the location of Python module sources?
For a pure python module you can find the source by looking at themodule.__file__
.
The datetime module, however, is written in C, and therefore datetime.__file__
points to a .so file (there is no datetime.__file__
on Windows), and therefore, you can't see the source.
If you download a python source tarball and extract it, the modules' code can be found in the Modules subdirectory.
For example, if you want to find the datetime code for python 2.6, you can look at
Python-2.6/Modules/datetimemodule.c
You can also find the latest version of this file on github on the web at
https://github.com/python/cpython/blob/main/Modules/_datetimemodule.c
Cannot find user site directory for Python site packages
There are 2 commands you can use to get the path to site packages:
The following will show you the path to the globally installed site packages
python -m site
and this command will show you the packages installed for that particular user
python -m site --user-site
In your case it might be that the packages are all installed globally and not for a user.
Finding python site-packages directory with CMake
You can execute external processes in cmake with execute_process (and get the output into a variable if needed, as it would be here).
Where is site-packages located in a Conda environment?
You can import the module and check the module.__file__
string. It contains the path to the associated source file.
Alternatively, you can read the File
tag in the the module documentation, which can be accessed using help(module)
, or module?
in IPython.
Where does pip install its packages?
pip when used with virtualenv will generally install packages in the path <virtualenv_name>/lib/<python_ver>/site-packages
.
For example, I created a test virtualenv named venv_test with Python 2.7, and the django
folder is in venv_test/lib/python2.7/site-packages/django
.
Related Topics
Use Numpy Array in Shared Memory For Multiprocessing
Append Existing Excel Sheet With New Dataframe Using Python Pandas
How to Get the Value of a Variable Given Its Name in a String
How to Get the Filename Without the Extension from a Path in Python
Confused by Python File Mode "W+"
Generating a Png With Matplotlib When Display Is Undefined
How to Programmatically Set an Attribute
Multiple Prints on the Same Line in Python
Pygame Doesn't Let Me Use Float For Rect.Move, But I Need It
Python Exit Commands - Why So Many and When Should Each Be Used
How to Convert Local Time String to Utc
How to Write to an Excel Spreadsheet Using Python
Why Is Python Ordering My Dictionary Like So
Install a Python Package into a Different Directory Using Pip
Typeerror: a Bytes-Like Object Is Required, Not 'Str' When Writing to a File in Python 3