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
.pthfile insite-packagesbeats globalPYTHONPATH:- Setting
PYTHONPATHglobally 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
.pthfile (any file ending in.pthinsidesite-packages, containing one absolute directory path per line) is read automatically by Python’s built-insitemodule only for that specific environment (venv,conda, or system Python).
- Setting
- Avoid
setx PYTHONPATH ...on Windows ifPYTHONPATHalready exists or exceeds 1,024 characters:- The Windows
setxCLI silently truncates values at 1,024 characters and overwrites existingPYTHONPATHentries 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.
- The Windows
- 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.
- Environment variable changes in
- How
sys.pathordersPYTHONPATHvs..pthfiles:- Directories in
PYTHONPATHare inserted before the standard library andsite-packages(right after the script’s own directorysys.path[0]), meaning a file namedjson.pyorlogging.pyinsidePYTHONPATHwill shadow and break Python’s standard library. - Directories added via
.pthfiles are appended after the standard library, preventing accidental stdlib shadowing.
- Directories in
- Best practice for reusable codebases (
pyproject.toml+pip install -e .):- Instead of manually managing paths across machines, add a minimal
pyproject.tomlto your library folder and runpython -m pip install -e /path/to/my_package. This registers an editable link cleanly insidesite-packagesso imports work everywhere without touchingPYTHONPATH.
- Instead of manually managing paths across machines, add a minimal
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. |
You must log in or register to comment.