The directory site-packages
is mentioned in various Python related articles. What is it? How can I use it?
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)
lib64
! –
Deform /usr/lib/python3.6
? –
Mccormick site-packages
. –
Airedale virtualenv --no-site-packages --python=/path/to/python/executable/python ENV_DIR_NAME
–
Capybara When you use --user
option with pip, the package gets installed in user's folder instead of global folder and you won't need to run pip command with admin privileges.
The location of user's packages folder can be found using:
python -m site --user-site
This will print something like:
C:\Users\%USERNAME%\AppData\Roaming\Python\Python35\site-packages
When you don't use --user
option with pip, the package gets installed in global folder given by:
python -c "import site; print(site.getsitepackages())"
This will print something like:
['C:\\Program Files\\Anaconda3', 'C:\\Program Files\\Anaconda3\\lib\\site-packages'
Note: Above printed values are for On Windows 10 with Anaconda 4.x installed with defaults.
site-packages
. –
Airedale site-packages
? –
Airedale --user
parameter in python seems valid for Windows but not Linux. In Linux everyone is a user even root
I think. –
Hyperacidity site-packages is just the location where Python installs its modules.
There isn't any need to "find it". Python knows where to find it by itself, and this location is always part of the PYTHONPATH (sys.path).
Programmatically you can find it this way:
import sys
site_packages = next(p for p in sys.path if 'site-packages' in p)
print(site_packages)
Output:
'/Users/foo/.envs/env1/lib/python3.11.1/site-packages'
site-packages
, and this will raise StopIteration
. For instance, Debian (and Ubuntu) have dist-packages
to install their distributed modules. –
Bruni On my CentOS 7.9 Linux (a Red Hat clone) it is found in ~/.local/lib/python3.9/site-packages/
and there isn't any need to include it in the PYTHONPATH variable.
Example: let's say we are using python for machine learning, we usually install multiple libraries such as numpy, pandas, tensorflow / torch, etc. When we do "pip install numpy", numpy gets installed. But every wondered where exactly these installed libraries go? This is where site-packages directory comes. site-packages directory stores every third party libraries that we use in our project. The most important thing is, this site-packages directory is not project-dependent. Its environment-dependent, i.e. you can have multiple project run being within a single v.environment and all these projects will have a same site-packages directory that is inside that environment.
According to here:
A Python installation has a site-packages directory inside the module directory. This directory is where user installed packages are dropped.
Though it doesn't explain why the word site is chosen, it explains what this directory is meant for.
v:latest
. Maybe it is latest. –
Tar https://packaging.python.org/en/latest/
would be a more recent reference. –
Biquarterly © 2022 - 2024 — McMap. All rights reserved.
/usr/local/lib/python3.6/site-packages
on ubuntu – Lichenology