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

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.

Intermediate35 min
Reusing Python packages hero Module 14 Lesson 9

Import packages from any folder using sys.path.append() or PYTHONPATH. 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 PYTHONPATH for machine-wide reuse
  • Choose the right approach for real projects

The problem

Suppose your package and application are in different folders.

Package and app in different folders import bankkit fails packages not found
text
/home/dev/
    packages/           ← bankkit/ lives here
        bankkit/
            bankinfo.py
    projects/
        myapp/
            main.py     ← your program runs here

Why 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

sys path append main.py adds packages folder bankkit found for this program only

Code

python
import sys

# Parent folder that contains bankkit/
sys.path.append("/home/dev/packages")

import bankkit.bankinfo

print(bankkit.bankinfo.BNAME)

What happens?

  1. Python starts main.py
  2. sys.path.append() adds a new search directory
  3. import bankkit.bankinfo succeeds
  4. 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

PYTHONPATH environment variable Project A and B both import bankkit

After setting PYTHONPATH, every Python program can import bankkit.

text
PYTHONPATH=/home/dev/packages

Then any project can simply do:

python
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

python
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

text
Welcome to bankkit, Developer
Bank: BOI
SI: 10000.0
sum = 30

Path format reminders

OS Absolute path examples
Linux / macOS /Users/you/dev/packages
Windows D:/dev/packages or D:\\dev\\packages
Append parent folder containing bankkit not bankkit itself

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:

text
Correct:  ✔ /home/dev/packages
Wrong:    ✘ /home/dev/packages/bankkit

Key takeaways

  • sys.path is Python's search path list
  • append() adds a folder for the current program only
  • PYTHONPATH makes 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