Lesson 09
Reusing Python Packages — sys.path.append and PYTHONPATH
Import packages from any folder using sys.path.append for one script or PYTHONPATH for machine-wide reuse — with bankkit examples.
Import packages from any folder using
sys.path.append()orPYTHONPATH. A package does not have to sit beside your project — Python only needs to know where to find it.
Use the playground and quiz to practice both approaches.
What you will learn
- Why imports fail across folders
- Use
sys.path.append()for one script - Use
PYTHONPATHfor machine-wide reuse - Choose the right approach for real projects
The problem
Suppose your package and application are in different folders.
/home/dev/
packages/ ← bankkit/ lives here
bankkit/
bankinfo.py
projects/
myapp/
main.py ← your program runs hereWhy does this fail? main.py is inside myapp, but bankkit lives in another folder. Python does not search /home/dev/packages automatically.
Solution 1 — sys.path.append()
This is the simplest solution for one script.
How it works
Code
import sys
# Parent folder that contains bankkit/
sys.path.append("/home/dev/packages")
import bankkit.bankinfo
print(bankkit.bankinfo.BNAME)What happens?
- Python starts
main.py sys.path.append()adds a new search directoryimport bankkit.bankinfosucceeds- The path disappears when the program ends
Best for: learning, Jupyter notebooks, and small scripts.
| Piece | Meaning |
|---|---|
sys |
Built-in module |
sys.path |
List of folders Python searches |
append(...) |
Add one folder for this program only |
| Path you pass | Folder containing bankkit/, not bankkit/ itself |
Solution 2 — PYTHONPATH
Instead of changing every script, configure your operating system once.
Concept
After setting PYTHONPATH, every Python program can import bankkit.
PYTHONPATH=/home/dev/packagesThen any project can simply do:
import bankkit.arithops
bankkit.arithops.sumop(10, 20)Best for: libraries reused across many projects.
Which one should you use?
| Situation | Recommended |
|---|---|
| One script | sys.path.append() |
| Jupyter Notebook | sys.path.append() |
| College projects | sys.path.append() |
| Personal reusable library | PYTHONPATH |
| Company production apps | Install the package (pip) |
Complete example
import sys
# Add package location
sys.path.append("/home/dev/packages")
import bankkit.bankinfo as bi
from bankkit.arithops import sumop
from bankkit import greetmod
greetmod.welcome("Developer")
print("Bank:", bi.BNAME)
bi.simple_interest(50000, 2, 10)
sumop(10, 20)Output
Welcome to bankkit, Developer
Bank: BOI
SI: 10000.0
sum = 30Path format reminders
| OS | Absolute path examples |
|---|---|
| Linux / macOS | /Users/you/dev/packages |
| Windows | D:/dev/packages or D:\\dev\\packages |
sys.path vs PYTHONPATH
| Feature | sys.path.append() |
PYTHONPATH |
|---|---|---|
| Scope | Current script | Entire machine |
| Permanent | No | Yes |
| Requires OS setup | No | Yes |
| Best for | Learning and testing | Reusable libraries |
Interview cheat sheet
Q: Why does import bankkit fail?
Python searches only its known directories. If the package is elsewhere, add its parent folder to the search path.
Q: Which is temporary?
sys.path.append()→ temporary (current program)PYTHONPATH→ permanent (until you change env vars)
Q: Which path should you append?
Append the folder containing the package, not the package itself:
Correct: ✔ /home/dev/packages
Wrong: ✘ /home/dev/packages/bankkitKey takeaways
sys.pathis Python's search path listappend()adds a folder for the current program onlyPYTHONPATHmakes packages available to all Python programs- Always provide the parent directory of your package
- Next: PYTHONPATH Global Setup — Windows, macOS Intel, Apple Silicon, Linux
Loading playground…
Check your understanding
Reusing packages quiz
sys.path is:
1 / 10