Python
Programming
Site-Packages Directory
Python Packages
Coding Tutorial

How to find your Python site-packages directory location

Codemia Knowledge Hub ·

Run python -m site or python -c "import site; print(site.getsitepackages())". The site-packages directory is where pip installs third-party packages for one specific Python interpreter, and its location depends on the operating system, the Python version, and whether you use a virtual environment, Conda or the system interpreter.

site-packages is not a machine-wide directory. It belongs to one interpreter installation, so if you query the wrong interpreter you get the wrong path even though the commands are correct.

Step 1: Confirm which interpreter is active

Check which Python you are actually running before asking for package paths. Getting this wrong is the most common cause of "I installed it but Python can't find it".

bash
1# Which binary is being invoked?
2which python3
3python3 -c "import sys; print(sys.executable)"
4
5# What version is it?
6python3 --version

If you use a virtual environment, activate it first:

bash
source .venv/bin/activate
python -c "import sys; print(sys.executable)"

On Windows:

cmd
.venv\Scripts\activate
python -c "import sys; print(sys.executable)"

The executable path tells you exactly which interpreter the following commands will query.

Step 2: Ask the site module

The built-in site module is the authoritative source for package directories:

python
1import site
2
3# Environment-level site-packages (where pip installs by default)
4print("Site-packages directories:")
5for path in site.getsitepackages():
6    print(f"  {path}")
7
8# Per-user site-packages (pip install --user)
9print(f"\nUser site-packages: {site.getusersitepackages()}")
10
11# Is user site enabled?
12print(f"User site enabled: {site.ENABLE_USER_SITE}")

Typical output on macOS with a virtual environment:

text
1Site-packages directories:
2  /Users/alice/.venv/lib/python3.12/site-packages
3
4User site-packages: /Users/alice/.local/lib/python3.12/site-packages
5User site enabled: False

From the command line

bash
python3 -m site

This prints sys.path, the user site-packages location and whether user site is enabled, which is the quickest complete picture.

For just the site-packages path:

bash
python3 -c "import site; print(site.getsitepackages()[0])"

Step 3: Use sysconfig for more detail

The sysconfig module gives finer-grained paths:

python
1import sysconfig
2
3# All configured paths
4paths = sysconfig.get_paths()
5for name, path in paths.items():
6    print(f"{name:15} -> {path}")

Output includes:

text
1stdlib          -> /usr/lib/python3.12
2platstdlib      -> /usr/lib/python3.12
3purelib         -> /usr/lib/python3.12/site-packages
4platlib         -> /usr/lib/python3.12/site-packages
5include         -> /usr/include/python3.12
6scripts         -> /usr/bin
7data            -> /usr

The key entries:

Path NameMeaning
purelibWhere pure Python packages are installed
platlibWhere platform-specific (C extension) packages are installed
scriptsWhere executable scripts (entry points) are placed
includeWhere C header files for package development are stored

purelib and platlib usually point to the same directory. Some Unix installations put architecture-specific packages in a separate location.

Finding where pip installed a package

pip show reports where a specific package lives:

bash
python -m pip show requests

Output:

text
Name: requests
Version: 2.31.0
Location: /Users/alice/.venv/lib/python3.12/site-packages

The Location field is the site-packages directory, which confirms the package went into the environment you expected.

To list every package with its location:

bash
python -m pip list -v

The verbose flag adds a Location column, which reveals packages split across directories (user site vs environment site).

Why python -m pip instead of pip

python -m pip always runs pip under the same interpreter as python. A bare pip can resolve to a different Python installation, especially when several versions are installed:

bash
1# These might point to different interpreters
2which pip
3which pip3
4which python3
5
6# This always matches
7python3 -m pip show pip

Typical paths by platform

Do not hardcode these, but recognizing the patterns helps when debugging:

PlatformVirtual EnvironmentSystem Python
Linux.venv/lib/python3.X/site-packages/usr/lib/python3.X/site-packages
macOS.venv/lib/python3.X/site-packages/Library/Frameworks/Python.framework/.../site-packages
macOS (Homebrew).venv/lib/python3.X/site-packages/opt/homebrew/lib/python3.X/site-packages
Windows.venv\Lib\site-packagesC:\PythonXX\Lib\site-packages
Condaenvs/myenv/lib/python3.X/site-packageslib/python3.X/site-packages (base env)

3.X follows your Python version. Upgrading Python (for example from 3.11 to 3.12) changes the site-packages directory too, which is why virtual environments are recreated after upgrades.

Checking paths inside notebooks and IDEs

Many import problems come from the terminal using one interpreter while a Jupyter notebook or IDE uses another. Check inside the environment where the problem happens:

python
1# Run this in a Jupyter notebook cell
2import sys
3import site
4
5print(f"Interpreter: {sys.executable}")
6print(f"Version: {sys.version}")
7print(f"Site-packages: {site.getsitepackages()}")
8print(f"\nsys.path:")
9for p in sys.path:
10    print(f"  {p}")

If the notebook's interpreter is not the one you expected, fix the Jupyter kernel or IDE configuration rather than reinstalling packages.

Registering a virtual environment as a Jupyter kernel

bash
1# Install kernel for a specific virtual environment
2source .venv/bin/activate
3pip install ipykernel
4python -m ipykernel install --user --name=myproject --display-name="Python (myproject)"

Then select the "Python (myproject)" kernel in Jupyter to use that environment's packages.

An environment diagnostic script

For teams that often debug environment issues, a small script saves a lot of time:

python
1#!/usr/bin/env python3
2"""Print Python environment details for debugging."""
3
4import os
5import site
6import sys
7import sysconfig
8
9print(f"Executable:  {sys.executable}")
10print(f"Version:     {sys.version}")
11print(f"Platform:    {sys.platform}")
12print(f"Prefix:      {sys.prefix}")
13print(f"Base prefix: {sys.base_prefix}")
14print(f"Virtual env: {sys.prefix != sys.base_prefix}")
15print(f"Purelib:     {sysconfig.get_paths()['purelib']}")
16print(f"User site:   {site.getusersitepackages()}")
17print()
18
19# Check if a specific package is importable and where it lives
20for pkg_name in ['requests', 'numpy', 'flask']:
21    try:
22        mod = __import__(pkg_name)
23        location = getattr(mod, '__file__', 'built-in')
24        print(f"  {pkg_name}: {location}")
25    except ImportError:
26        print(f"  {pkg_name}: NOT INSTALLED")

Run it in each context (terminal, notebook, Docker container, CI job) to see at once whether the environments match.

Virtual environment vs user site vs system site

Packages can be installed in three places:

LocationCommandTypical Use
Virtual environment site-packagespip install pkg (inside venv)Project-specific dependencies
User site-packagespip install --user pkgPer-user packages without sudo
System site-packagessudo pip install pkgSystem-wide (generally discouraged)

Python searches these in order when resolving imports, so a package in the virtual environment shadows the same package in user or system site-packages.

bash
# See the full search order
python -c "import sys; print('\n'.join(sys.path))"

Mistakes that give you the wrong path

  • Mixing interpreters. Querying pip from one interpreter and running code with another is the top cause of "package not found". Tie the query to the interpreter with python -m pip.
  • Assuming one machine-wide directory. Virtual environments, user installs and Conda environments mean one machine can have dozens of independent site-packages directories.
  • Hardcoding guessed paths. A path like /usr/lib/python3/site-packages breaks when the Python version, the OS or the environment layout changes. Query it programmatically.
  • Forgetting to activate the virtual environment. You get the system Python's site-packages instead of the project's. sys.prefix != sys.base_prefix tells you whether a virtual environment is active.
  • Using sudo pip install. Installing into the system Python can break OS tools that depend on specific package versions. Use virtual environments for projects and --user for personal tools.

Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Basis Lab
No course covers this one yet

Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.

Build my course
Data Structures & Algorithms practice on Codemia

Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.

Practice algorithms