Lesson 10
PYTHONPATH Global Setup — Windows, macOS & Linux
Set PYTHONPATH permanently on Windows, macOS Intel, Apple Silicon, and Linux so your packages import from any project without sys.path.append.
Set
PYTHONPATHonce so Python can import your own packages (bankkit,arithops, etc.) from any project folder withoutsys.path.append().
Use the playground and quiz to confirm how sys.path is built.
What you will learn
- Set
PYTHONPATHpermanently on Windows - Configure it on macOS Intel and Apple Silicon
- Configure it on Linux
- Verify that Python can find your packages
- Understand
PYTHONPATHvssys.path.append()
Platform overview
Python builds sys.path from:
- The script's directory
- Standard library paths
- Directories listed in
PYTHONPATH - Site-packages (pip installs)
Before setting PYTHONPATH
Assume this folder structure:
Important: set the path to the parent folder (packages/), not the bankkit/ folder.
| ✅ Correct | ❌ Wrong |
|---|---|
/Users/you/dev/packages |
/Users/you/dev/packages/bankkit |
D:\\dev\\packages |
D:\\dev\\packages\\bankkit |
Windows setup (permanent)
Step 1 — Open Environment Variables
Press Win + R, type sysdm.cpl, and press Enter.
Step 2 — Create PYTHONPATH
Under User variables, click New.
| Field | Value |
|---|---|
| Variable name | PYTHONPATH |
| Variable value | D:\\dev\\packages |
Step 3 — Restart terminal
Close and reopen CMD, PowerShell, or VS Code.
Step 4 — Verify
echo $env:PYTHONPATH
python -c "import sys; print(sys.path)"
python -c "import bankkit.bankinfo as bi; print(bi.BNAME)"Multiple folders (Windows): separate with semicolon ;
D:\dev\packages;D:\shared\libsmacOS (Intel & Apple Silicon)
Both Intel and M-series (Apple Silicon) Macs use the same method — only the path string and Python install location may differ.
Step 1 — Open Terminal
Step 2 — Edit .zshrc
nano ~/.zshrcAdd this line:
export PYTHONPATH="/Users/you/dev/packages"Step 3 — Save and reload
source ~/.zshrcStep 4 — Verify
echo $PYTHONPATH
python3 -c "import bankkit.bankinfo as bi; print(bi.BNAME)"Apple Silicon tip: use the same python3 in Terminal and VS Code (often /opt/homebrew/bin/python3).
Intel Mac still on bash? put the same export in ~/.bash_profile.
Multiple folders (macOS / Linux): separate with colon :
export PYTHONPATH="/Users/you/dev/packages:/Users/you/shared/libs"Linux setup
Step 1 — Edit .bashrc
nano ~/.bashrcAdd:
export PYTHONPATH="/home/you/dev/packages"Step 2 — Reload
source ~/.bashrcStep 3 — Verify
echo $PYTHONPATH
python3 -c "import bankkit.bankinfo as bi; print(bi.BNAME)"System-wide (all users): add to /etc/environment, then log out and back in.
How Python finds your package
When Python starts, it automatically adds the PYTHONPATH directory into sys.path. Then this works from any folder:
import bankkit.bankinfo as bi
from bankkit.arithops import sumop
print(bi.BNAME)
sumop(10, 20)Super example — end to end
1. Create the package (/home/you/dev/packages/bankkit/):
packages/
bankkit/
__init__.py
bankinfo.py
arithops.py2. Set PYTHONPATH to /home/you/dev/packages using the steps above for your OS.
3. Run from anywhere:
# ~/Desktop/demo/main.py — no sys.path.append!
import bankkit.bankinfo as bi
from bankkit.arithops import sumop
print(bi.BNAME)
sumop(100, 250)Verify inside VS Code
After setting PYTHONPATH, restart VS Code so the IDE inherits the environment variable.
import sys
import bankkit.bankinfo as bi
print("Bank:", bi.BNAME)
print("First search path:", sys.path[0])If PYTHONPATH is configured correctly, the import succeeds without any sys.path.append().
sys.path.append vs PYTHONPATH
| Feature | sys.path.append() |
PYTHONPATH |
|---|---|---|
| Scope | One script | Entire machine |
| Permanent | No | Yes |
| Written in | Python code | OS environment |
| Best for | Testing and notebooks | Real projects |
| Need in every script? | Yes | No |
Use sys.path.append() (temporary)
import sys
sys.path.append("/Users/you/dev/packages")
import bankkit.bankinfoUse PYTHONPATH (recommended)
# No extra path code needed
import bankkit.bankinfo
import bankkit.arithopsInterview cheat sheet
| Question | Answer |
|---|---|
What is PYTHONPATH? |
Environment variable that adds directories to Python's module search path |
| What path should you set? | The parent folder containing your package |
| Windows separator | ; between multiple paths |
| macOS/Linux separator | : between multiple paths |
| Temporary alternative | sys.path.append() |
| Apple Silicon vs Intel? | Same export PYTHONPATH=... — path differs per user |
Module 14 — complete journey
1. Modules overview
2. Custom modules
3. import — 4 syntaxes
4. from … import — 3 syntaxes
5. importlib.reload
6. Multi-module project
7. Packages overview
8. Creating packages
9. Reusing packages
10. PYTHONPATH global setup ← you are hereThis completes Module 14: Modules & Packages — you can now build reusable modules, organize them into packages, and import them across real-world Python projects.
Key takeaways
- Set
PYTHONPATHto the folder containing your package folders - Windows:
sysdm.cpl→ Environment Variables → NewPYTHONPATH - macOS (Intel + Apple Silicon):
export PYTHONPATH=...in~/.zshrc - Linux:
export PYTHONPATH=...in~/.bashrc - Always restart the terminal / VS Code and verify with
echo $PYTHONPATHand a test import
Loading playground…
Check your understanding
PYTHONPATH global setup quiz
PYTHONPATH value should point to:
1 / 10