215

The directory site-packages is mentioned in various Python related articles. What is it? How to use it?

vbence
  • 20,084
  • 9
  • 69
  • 118
Omer Dagan
  • 14,868
  • 16
  • 44
  • 60

5 Answers5

142

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

informatik01
  • 16,038
  • 10
  • 74
  • 104
Omer Dagan
  • 14,868
  • 16
  • 44
  • 60
  • 16
    location happened to be `/usr/local/lib/python3.6/site-packages` on ubuntu – mehmet May 27 '17 at 20:16
  • I've seen Gentoo systems with it in `lib64`! – MultipleMonomials Nov 10 '17 at 04:24
  • 3
    does conda or pip install into site-packages, or just manually built packages? – Monica Heddneck Dec 15 '17 at 23:42
  • 8
    The really interesting question is: Why this directory? Why not just install to `/usr/lib/python3.6`? – Torsten Bronger Dec 18 '17 at 07:43
  • Is it safe to delete it? I always find that installing packages into a specific environment doesn't work, since it is found unser `site-packages`. – MJimitater Jan 11 '21 at 20:14
  • 1
    @MJimitater I wouldn't do that. I think it's probably better to create the virtual environment and specify --no-site-packages (Which SHOULD be the default but might not be in your situation for various reasons.) `virtualenv --no-site-packages --python=/path/to/python/executable/python ENV_DIR_NAME` – George Griffin May 11 '21 at 21:03
80

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.

Shital Shah
  • 63,284
  • 17
  • 238
  • 185
37

site-packages is just the location where Python installs its modules.

No need to "find it", python knows where to find it by itself, 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)

'/Users/foo/.envs/env1/lib/python3.11.1/site-packages'

Ben Creasy
  • 3,825
  • 4
  • 40
  • 50
DevLounge
  • 8,313
  • 3
  • 31
  • 44
  • 5
    Probably easier to import site then site.getsitepackages() – JSharm Apr 23 '18 at 14:21
  • 1
    Not all Python distributions have `site-packages`, and this will raise `StopIteration`. For instance, Debian (and Ubuntu) have `dist-packages` to install their distributed modules. – Mike T Oct 22 '18 at 22:43
  • If you use virtualenv and do not inherit global packages then this will always work. I never code without a virtualenv – DevLounge Oct 23 '18 at 17:47
0

On my CentOS7.9 Linux (a RedHat clone) it is found in ~/.local/lib/python3.9/site-packages/ and there is no need to include it in the PYTHONPATH variable.

karsten
  • 639
  • 5
  • 13
-1

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.

smwikipedia
  • 61,609
  • 92
  • 309
  • 482
  • That link is from 2009; would you of anything newer, and clear and concise ? – denis Jan 31 '22 at 15:08
  • @denis On the bottom left of that link, it says `v:latest`. Maybe it is latest. – smwikipedia Feb 02 '22 at 04:19
  • ["Hitchhiker’s Guide to Packaging" is no longer maintained"](https://github.com/python/pythondotorg/issues/268) ... the bottom of the ".../latest/..." URL page shows copyright 2009. Something on `https://packaging.python.org/en/latest/` would be a more recent reference. – marc-medley Feb 01 '23 at 01:01