How do you locate your Python site-packages installation directory? | A1Lab@MZeeshanM to PythonEnglish • 4 days ago

How do you locate your Python site-packages installation directory?

message-square
0

Run python -m site in your terminal or inspect sysconfig.get_paths()["purelib"] (or site.getsitepackages()) inside Python—making sure to run the command with the exact Python interpreter or active virtual environment whose site-packages folder you want to locate.

Basic Example

# 1. Terminal one-liner: Prints sys.path, USER_SITE, and ENABLE_USER_SITE status
python -m site

# 2. Print ONLY the user-specific site-packages path
python -m site --user-site
import site
import sysconfig

# 3. Recommended in Python scripts: sysconfig (works in global Python AND venvs)
print(sysconfig.get_paths()["purelib"])   # Pure-Python packages directory
print(sysconfig.get_paths()["platlib"])   # Platform-specific C-extension packages directory

# 4. List all global/environment site-packages directories
print(site.getsitepackages())

# 5. Locate the exact site-packages directory containing a specific installed library
import requests
print(requests.__file__)  # e.g., "/path/to/.venv/lib/python3.12/site-packages/requests/__init__.py"

Key Details & Edge Cases

  • sysconfig.get_paths()["purelib"] vs. site.getsitepackages():
    • sysconfig.get_paths()["purelib"] returns the exact primary site-packages string across Windows, macOS, Linux, virtual environments (venv), and Conda environments without platform inconsistencies.
    • site.getsitepackages() returns a list[str] that can include extra system directories (such as /usr/lib/python3/dist-packages alongside /usr/local/lib/python3.12/dist-packages on Debian/Ubuntu Linux).
  • Debian / Ubuntu dist-packages vs. site-packages:
    • On Debian and Ubuntu systems, the OS-bundled Python installs third-party APT and global pip packages into dist-packages (/usr/lib/python3/dist-packages or /usr/local/lib/python3.X/dist-packages), whereas virtual environments (venv) created on those same machines always use site-packages (.venv/lib/python3.X/site-packages).
  • Windows vs. POSIX directory layouts:
    • Windows: <prefix>\Lib\site-packages (e.g., C:\Users\Alice\project\.venv\Lib\site-packages).
    • Linux / macOS: <prefix>/lib/pythonX.Y/site-packages (e.g., /home/alice/project/.venv/lib/python3.12/site-packages).
  • User site-packages (--user installs): Packages installed with pip install --user go into a separate user-scoped folder (site.getusersitepackages()), typically ~/.local/lib/pythonX.Y/site-packages on Linux/macOS or %APPDATA%\Python\PythonXY\site-packages on Windows—note that standard venv environments disable USER_SITE (ENABLE_USER_SITE: False) by default for isolation.
  • Finding where a specific package lives (pip show <pkg>): If multiple site-packages directories exist on sys.path, run python -m pip show <package-name> and check the Location: field to see which exact directory loaded that package.

Comparison of site-packages Lookup Methods

Method Environment Output Type Typical Output Example Best Used When
python -m site Terminal CLI Printed diagnostic report sys.path = [...], USER_SITE: '...' Quickly inspecting all active search paths and user-site status from a shell.
sysconfig.get_paths()["purelib"] Python code str "/app/.venv/lib/python3.12/site-packages" Programmatically resolving the primary site-packages path reliably across all OSes and venvs.
site.getsitepackages() Python code list[str] ["/usr/local/lib/python3.12/dist-packages", ...] Enumerating all environment-level site/dist-packages directories.
site.getusersitepackages() Python code str "/home/user/.local/lib/python3.12/site-packages" Locating user-level (pip install --user) packages outside a virtual environment.
python -m pip show <pkg> Terminal CLI Metadata block (Location: ...) Location: /app/.venv/lib/python3.12/site-packages Verifying which exact site-packages folder a specific installed package resides in.