Question

Find the Python site-packages Directory

Question

How can I find the location of the site-packages directory used by my current Python installation?

Short Answer

You will learn what Python's site-packages directory is, why its location varies, and how to find the correct global, virtual-environment, or user-specific directory from Python and the command line.

Concept

Python installs most third-party packages, such as requests, Flask, or numpy, into a directory commonly named site-packages. When Python imports a package, it searches a list of directories stored in sys.path; applicable site-packages directories are often on that list.

There is no single universal location. The path depends on:

  • Your operating system
  • The Python installation being used
  • Whether a virtual environment is active
  • Whether a package was installed for the current user
  • The installation method, such as a system Python, Homebrew Python, or Conda

For reliable code, prefer Python's standard-library APIs instead of constructing paths such as /usr/lib/python3.x/site-packages yourself. sysconfig reports installation paths for the exact interpreter that runs your code.

Mental Model

Think of each Python interpreter as a separate workshop with its own supply shelves.

  • The interpreter is the workshop.
  • A virtual environment is a smaller private workshop.
  • site-packages is a shelf containing externally installed tools (packages).
  • sysconfig is the workshop directory map.

If you ask the wrong workshop where its shelf is, you may install a package in one place and try to import it from another. That is why commands should usually start with python -m pip: the python interpreter and pip tool are then tied together.

Take Quiz

Syntax and Examples

Use sysconfig to ask the currently running Python interpreter for its package installation directories:

import sysconfig

print(sysconfig.get_path("purelib"))
print(sysconfig.get_path("platlib"))
  • purelib is the directory for pure-Python packages.
  • platlib is the directory for packages that may include platform-specific compiled code.
  • On many installations they are the same directory, but do not assume that they always are.

To run this without creating a file:

python -c "import sysconfig; print(sysconfig.get_path('purelib'))"

Python's site module can also report package directories:

import site

print(site.getsitepackages())
print(site.getusersitepackages())

site.getsitepackages() returns a list of global site-package directories when available. site.getusersitepackages() returns the per-user package directory, which is relevant when packages are installed with pip install --user.

To see where a particular installed package lives, use the same interpreter to run pip:

python -m pip show requests

Look for the Location: line in the output.

Step by Step Execution

Consider this script:

import sys
import sysconfig

print(sys.executable)
print(sysconfig.get_path("purelib"))

Execution proceeds as follows:

  1. import sys makes interpreter information available.
  2. import sysconfig loads Python's installation-path configuration tools.
  3. sys.executable prints the full path to the Python program currently executing the script. This identifies which Python installation is being queried.
  4. sysconfig.get_path("purelib") reads that interpreter's configuration and returns its pure-Python package directory.

For example, if sys.executable points inside .venv, the returned site-packages path will normally also be inside .venv. If it points to a system Python, the path will normally belong to that system installation.

Real World Use Cases

Common uses include:

  • Debugging ModuleNotFoundError: Confirm whether a dependency was installed into the same interpreter that runs the application.
  • Virtual-environment checks: Verify that a deployment script is using the project virtual environment instead of a global Python.
  • Installation diagnostics: Find the directory where pip placed a package when investigating version conflicts.
  • Developer tooling: Build a diagnostic command that reports Python executable, version, and package directories.
  • User-level installs: Determine where packages installed with pip install --user are stored.
  • CI and containers: Diagnose differences between local and automated build environments without hard-coding operating-system paths.
Take Quiz

Real Codebase Usage

Application code rarely needs to write directly into site-packages. Package managers own that directory, and modifying it can break installations or be overwritten by upgrades.

Instead, projects commonly use these patterns:

  • Environment diagnostics: Log sys.executable and sysconfig.get_path("purelib") when a command cannot import a dependency.
  • Correct pip invocation: Use python -m pip install -r requirements.txt, not a bare pip install ..., so installation targets the intended interpreter.
  • Dependency validation: Fail early with a useful message if an optional package cannot be imported.
import sys

try:
    import yaml
except ImportError as error:
    raise SystemExit(
        f"PyYAML is missing for interpreter: {sys.executable}\n"
        "Install it with: python -m pip install pyyaml"
    ) from error
  • Configuration instead of hard-coded paths: Store project data in application directories, temporary directories, or paths supplied by configuration. Do not treat site-packages as application storage.
  • Inspection tools: Use importlib.metadata to inspect installed distributions rather than scanning directories manually.
from importlib.metadata import version

print(version("requests"))

Common Mistakes

Assuming one fixed path

This is not portable:

# Broken: works only for one particular installation layout.
packages = "/usr/lib/python3.12/site-packages"

Use sysconfig.get_path("purelib") instead.

Using pip from a different Python installation

A bare pip command may refer to a different installation than python:

pip install requests
python app.py

If app.py cannot import requests, install through the same interpreter:

python -m pip install requests

Expecting site.getsitepackages() to be one string

It returns a list, and there can be more than one directory:

import site

paths = site.getsitepackages()
for path in paths:
    print(path)

Confusing the user site with the active virtual environment

site.getusersitepackages() is a user-level location. A package installed there may not be visible in every environment, depending on environment settings. Check the active interpreter and use python -m pip.

Adding paths to sys.path as a first fix

Manually changing import paths can hide an environment setup problem:

# Usually avoid this as a dependency-installation solution.
import sys
sys.path.append("/some/guessed/site-packages")

Install the dependency into the correct environment instead.

Comparisons

Tool or valueBest useImportant detail
sysconfig.get_path("purelib")Find the standard pure-Python install directory programmaticallyUsually the best default for scripts and tools.
sysconfig.get_path("platlib")Find the platform-specific package directoryMay equal purelib, but can differ.
site.getsitepackages()Inspect global site-package directoriesReturns a list; availability can depend on the Python environment.
site.getusersitepackages()Find the per-user package directoryRelevant to pip install --user.
sys.pathSee directories Python currently searches for importsContains more than site-packages, including the script directory and standard library paths.
python -m pip show PACKAGELocate one installed distributionShows pip's recorded Location, not every import path.

sys.path answers “Where will this Python try to import from?” while sysconfig answers “Where is this Python configured to install packages?”. Those questions overlap but are not identical.

Take Quiz

Cheat Sheet

# Recommended: current interpreter's pure-Python package directory
import sysconfig
print(sysconfig.get_path("purelib"))

# Platform-specific package directory
print(sysconfig.get_path("platlib"))

# Global site-package directories
import site
print(site.getsitepackages())

# Per-user package directory
print(site.getusersitepackages())

# All active import search paths
import sys
print("\n".join(sys.path))

# Interpreter identity
print(sys.executable)
# Print the current interpreter's main package directory
python -c "import sysconfig; print(sysconfig.get_path('purelib'))"

# Locate one installed distribution
python -m pip show PACKAGE_NAME

# Confirm which pip belongs to this Python
python -m pip --version

Rules to remember:

  • Use python -m pip, especially when multiple Python installations exist.
  • A virtual environment normally has its own site-packages directory.
  • Do not hard-code a site-packages path.
  • purelib and platlib can be different.
  • site-packages is not a safe place for application data files.

FAQ

How do I print the site-packages path in Python?

Use:

import sysconfig
print(sysconfig.get_path("purelib"))

Why does Python say a package is missing after pip installed it?

Usually pip installed the package for a different Python interpreter or virtual environment. Compare python -m pip --version with python -c "import sys; print(sys.executable)".

What is the difference between site-packages and sys.path?

site-packages is typically a directory containing installed third-party packages. sys.path is the full list of directories Python searches during imports, and it includes several other locations.

How do I find the site-packages directory in a virtual environment?

Activate the environment, then run:

python -c "import sysconfig; print(sysconfig.get_path('purelib'))"

The output should normally be inside that environment.

Is site.getsitepackages() always the best option?

For installation-path information, sysconfig.get_path("purelib") is generally a more direct choice. site.getsitepackages() is useful when you specifically want the list of global site directories.

Can I put my own files in site-packages?

Avoid it. Package managers manage that directory, and updates may remove or alter its contents. Keep application files in your project or a proper data directory.

Where does pip install --user install packages?

It targets the user site directory, which you can print with:

import site
print(site.getusersitepackages())
Take Quiz

Mini Project

Description

Create a small Python environment inspector. It reports the active interpreter, its main package-installation directories, the user package directory, and the import search path. This is useful when diagnosing missing packages or virtual-environment confusion.

Goal

Build a command-line script that shows where the current Python installation imports and installs packages.

Requirements

  • Print the active Python executable path.
  • Print the purelib and platlib directories.
  • Print the user site-packages directory.
  • Print every entry in sys.path.
  • Run the script with the Python interpreter you want to inspect.
Take Quiz

Keep learning

Related questions

View more