Drop a .pth file containing the directory’s absolute path inside your active Python environment’s site-packages directory (the cleanest, OS-agnostic, per-environment method), or set the PYTHONPATH environment variable permanently in your shell profile (~/.bashrc / ~/.zshrc on Linux/macOS) or Windows System Environment Variables (setx PYTHONPATH ...)—while preferring an editable package install (pip install -e .) for shareable Python projects.

Basic Example

# 1. Recommended (per-environment, zero global side effects):
#    Find your active environment's site-packages path and write a .pth file:
python -c "import site, pathlib; p = pathlib.Path(site.getsitepackages()[0]) / 'my_paths.pth'; p.write_text('/absolute/path/to/my_modules\n'); print('Wrote:', p)"

# 2. Linux / macOS (user-wide shell profile):
echo 'export PYTHONPATH="/absolute/path/to/my_modules${PYTHONPATH:+:$PYTHONPATH}"' >> ~/.zshrc  # or ~/.bashrc
source ~/.zshrc

# 3. Windows PowerShell (persistent user-level environment variable):
[Environment]::SetEnvironmentVariable("PYTHONPATH", "C:\absolute\path\to\my_modules", "User")

Key Details & Edge Cases

  • Why a .pth file in site-packages beats global PYTHONPATH:
    • Setting PYTHONPATH globally applies to every Python interpreter and virtual environment on your machine—frequently causing subtle version conflicts when Python 3.11 and Python 3.13 import incompatible modules or C extensions from the same shared path.
    • A .pth file (any file ending in .pth inside site-packages, containing one absolute directory path per line) is read automatically by Python’s built-in site module only for that specific environment (venv, conda, or system Python).
  • Avoid setx PYTHONPATH ... on Windows if PYTHONPATH already exists or exceeds 1,024 characters:
    • The Windows setx CLI silently truncates values at 1,024 characters and overwrites existing PYTHONPATH entries unless you append carefully. Using PowerShell’s [Environment]::SetEnvironmentVariable(..., "User") (or the Windows GUI: Edit the system environment variables $\rightarrow$ Environment Variables $\rightarrow$ User variables) avoids the 1,024-character truncation bug.
  • Remember to restart terminals / IDEs after editing PYTHONPATH:
    • Environment variable changes in ~/.zshrc, ~/.bashrc, or Windows User Variables do not affect already-open terminals, VS Code windows, or background services until you open a new shell session or restart the IDE.
  • How sys.path orders PYTHONPATH vs. .pth files:
    • Directories in PYTHONPATH are inserted before the standard library and site-packages (right after the script’s own directory sys.path[0]), meaning a file named json.py or logging.py inside PYTHONPATH will shadow and break Python’s standard library.
    • Directories added via .pth files are appended after the standard library, preventing accidental stdlib shadowing.
  • Best practice for reusable codebases (pyproject.toml + pip install -e .):
    • Instead of manually managing paths across machines, add a minimal pyproject.toml to your library folder and run python -m pip install -e /path/to/my_package. This registers an editable link cleanly inside site-packages so imports work everywhere without touching PYTHONPATH.

Comparison of Methods for Permanently Adding a Directory to Python’s Import Path

Method Scope Shadows Standard Library? Survives New Virtual Environments? Best Used When
.pth file in site-packages (Recommended) Single Python / .venv No (appended after stdlib) No (per-environment) Adding local shared utility folders cleanly to a specific virtual environment without polluting others.
pip install -e /path/to/pkg Single Python / .venv No No (installed via requirements.txt) Developing multi-file Python libraries or internal packages that need clean, portable imports.
Shell Profile (~/.zshrc / ~/.bashrc) All interactive shells for current OS user Yes (inserted ahead of stdlib) Yes (global across all .venvs) Linux/macOS workflows where a personal script folder must be importable in every terminal session.
Windows User Environment Variable Current Windows user (new processes) Yes (inserted ahead of stdlib) Yes (global across all .venvs) Windows machines needing a persistent user-wide module search path across IDEs and terminals.