Skip to main content
MahiDev — Learn. Build. Become AI-ready.

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.

Intermediate40 min
PYTHONPATH global setup hero Module 14 Lesson 10

Set PYTHONPATH once so Python can import your own packages (bankkit, arithops, etc.) from any project folder without sys.path.append().

Use the playground and quiz to confirm how sys.path is built.

What you will learn

  • Set PYTHONPATH permanently on Windows
  • Configure it on macOS Intel and Apple Silicon
  • Configure it on Linux
  • Verify that Python can find your packages
  • Understand PYTHONPATH vs sys.path.append()

Platform overview

PYTHONPATH setup Windows macOS Intel Apple Silicon Linux overview

Python builds sys.path from:

  1. The script's directory
  2. Standard library paths
  3. Directories listed in PYTHONPATH
  4. Site-packages (pip installs)

Before setting PYTHONPATH

Assume this folder structure:

Folder structure packages bankkit and projects myapp main py

Important: set the path to the parent folder (packages/), not the bankkit/ folder.

Correct PYTHONPATH parent folder vs wrong 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.

Windows Run dialog type sysdm.cpl
Windows System Properties Advanced tab Environment Variables

Step 2 — Create PYTHONPATH

Under User variables, click New.

Windows Environment Variables user variables click New
Windows New User Variable PYTHONPATH D dev packages
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

powershell
echo $env:PYTHONPATH
python -c "import sys; print(sys.path)"
python -c "import bankkit.bankinfo as bi; print(bi.BNAME)"
Windows PowerShell verify PYTHONPATH and import bankkit

Multiple folders (Windows): separate with semicolon ;

text
D:\dev\packages;D:\shared\libs

macOS (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

macOS Terminal open nano zshrc export PYTHONPATH

Step 2 — Edit .zshrc

bash
nano ~/.zshrc

Add this line:

bash
export PYTHONPATH="/Users/you/dev/packages"
macOS nano editing zshrc with export PYTHONPATH line

Step 3 — Save and reload

bash
source ~/.zshrc

Step 4 — Verify

bash
echo $PYTHONPATH
python3 -c "import bankkit.bankinfo as bi; print(bi.BNAME)"
macOS Terminal verify PYTHONPATH and bankkit import BOI

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 :

bash
export PYTHONPATH="/Users/you/dev/packages:/Users/you/shared/libs"

Linux setup

Step 1 — Edit .bashrc

bash
nano ~/.bashrc

Add:

bash
export PYTHONPATH="/home/you/dev/packages"
Linux nano bashrc export PYTHONPATH and reload

Step 2 — Reload

bash
source ~/.bashrc

Step 3 — Verify

bash
echo $PYTHONPATH
python3 -c "import bankkit.bankinfo as bi; print(bi.BNAME)"
Linux terminal verify PYTHONPATH import bankkit BOI

System-wide (all users): add to /etc/environment, then log out and back in.


How Python finds your package

main.py PYTHONPATH flows to packages bankkit folder sys path

When Python starts, it automatically adds the PYTHONPATH directory into sys.path. Then this works from any folder:

python
import bankkit.bankinfo as bi
from bankkit.arithops import sumop

print(bi.BNAME)
sumop(10, 20)

Super example — end to end

Super example create bankkit set PYTHONPATH import anywhere

1. Create the package (/home/you/dev/packages/bankkit/):

text
packages/
    bankkit/
        __init__.py
        bankinfo.py
        arithops.py

2. Set PYTHONPATH to /home/you/dev/packages using the steps above for your OS.

3. Run from anywhere:

python
# ~/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.

VS Code main.py import bankkit bankinfo successful output Bank BOI
python
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)

python
import sys
sys.path.append("/Users/you/dev/packages")
import bankkit.bankinfo
python
# No extra path code needed
import bankkit.bankinfo
import bankkit.arithops

Interview 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

text
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 here

This 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 PYTHONPATH to the folder containing your package folders
  • Windows: sysdm.cpl → Environment Variables → New PYTHONPATH
  • macOS (Intel + Apple Silicon): export PYTHONPATH=... in ~/.zshrc
  • Linux: export PYTHONPATH=... in ~/.bashrc
  • Always restart the terminal / VS Code and verify with echo $PYTHONPATH and a test import

Loading playground…

Check your understanding

PYTHONPATH global setup quiz

PYTHONPATH value should point to:

1 / 10