
Python
Python searches for modules in specific locations called import paths. Understanding how these paths work is essential for organizing larger projects.
When you `import module_name`, Python searches for it in this order:
1. Built-in modules - Modules compiled into Python (sys, math, json)
2. Current directory - The directory of the script being run
3. PYTHONPATH environment variable - Directories you've added
4. Installation-dependent default paths - Site-packages, dist-packages
import sys
# View all search paths
print(sys.path)
# ['/Users/username/projects',
# '/usr/local/lib/python3.9/site-packages',
# '/usr/lib/python3.9',
# ...]`sys.path` is a list of directories where Python looks for modules.
import sys
# Print each path on a new line
for path in sys.path:
print(path)
# Add a new directory to the search path
sys.path.append("/Users/username/my_modules")
# Now Python will search in that directory
# import my_custom_module # Will find it in /Users/username/my_modules
# Insert at beginning to give priority
sys.path.insert(0, "/Users/username/priority_modules")Simple Structure:
project/
āāā main.py
āāā math_utils.py# In main.py
import math_utils # Python finds it in current directory
result = math_utils.add(5, 3)Nested Structure:
project/
āāā main.py
āāā modules/
āāā __init__.py
āāā math_utils.py# In main.py
# Method 1: Add directory to sys.path
import sys
sys.path.append("modules")
import math_utils # Now found in modules/
# Method 2: Package import (if __init__.py exists)
from modules import math_utils
# Method 3: Direct import
from modules.math_utils import addExample 1: Adding Custom Module Directories
import sys
import os
# Get the directory of the current script
script_dir = os.path.dirname(os.path.abspath(__file__))
# Add a modules subdirectory
modules_dir = os.path.join(script_dir, "modules")
sys.path.insert(0, modules_dir)
# Now import from that directory
import custom_moduleExample 2: Adding Parent Directory
import sys
import os
# Go up one directory
parent_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, parent_dir)
# Now can import from parent
from shared_modules import utilitiesProject Structure:
myproject/
āāā main.py
āāā config/
ā āāā __init__.py
ā āāā settings.py
āāā lib/
ā āāā __init__.py
ā āāā database.py
ā āāā api_client.py
āāā scripts/
āāā setup.py
āāā migrate.pyIn main.py:
# As a package, use absolute imports
from config import settings
from lib import database, api_client
db = database.Database(settings.DB_HOST)
api = api_client.APIClient(settings.API_KEY)In scripts/setup.py (outside main package):
import sys
import os
# Add parent directory to path
parent = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, parent)
from config import settings
from lib import database
# Initialize database
db = database.Database(settings.DB_HOST)
db.init()Set the `PYTHONPATH` environment variable to add directories permanently.
# Add a single directory
export PYTHONPATH="/Users/username/my_modules:$PYTHONPATH"
# Add multiple directories (colon-separated)
export PYTHONPATH="/path1:/path2:/path3:$PYTHONPATH"
# Run Python with modified PYTHONPATH
PYTHONPATH="/Users/username/my_modules" python main.pyIn Python:
import os
import sys
# Check PYTHONPATH
pythonpath = os.environ.get("PYTHONPATH", "")
print(f"PYTHONPATH: {pythonpath}")
# These directories will be in sys.path
print("sys.path:", sys.path)When using packages (directories with `__init__.py`), the package directory becomes the root for imports.
company/
āāā __init__.py
āāā human_resources/
ā āāā __init__.py
ā āāā employee.py
ā āāā payroll.py
āāā finance/
āāā __init__.py
āāā accounting.py# company/human_resources/employee.py
from . import payroll # Import from same package
# company/human_resources/payroll.py
from ..finance import accounting # Go up, then into financeExample 1: Development Setup
# dev/project/app.py
import sys
import os
# Get absolute path of project root
PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, PROJECT_ROOT)
# Now can import everything from root
from lib import database
from models import User
from utils import validatorsExample 2: Virtual Environment Setup
# When using virtual environments, Python automatically adjusts sys.path
# venv/lib/python3.9/site-packages contains installed packages
# Your project modules should be in the project directory
# project/
# āāā main.py
# āāā modules/
# ā āāā helpers.py
# āāā venv/ (virtual environment)
import sys
print(sys.path) # Will include project directory automaticallyExample 3: Module Aliasing
import sys
# Create shortcuts for frequently used paths
sys.path["app"] = "/path/to/app/modules"
sys.path["lib"] = "/path/to/shared/lib"
# Use in imports
import app.models # Would work with proper setupIssue 1: "ModuleNotFoundError: No module named 'x'"
# ā Problem
import my_module # Module not in any search path
# ā
Solution 1: Check sys.path
import sys
print(sys.path)
print(os.path.exists("/path/to/my_module.py"))
# ā
Solution 2: Add directory
sys.path.append("/Users/username/modules")
import my_module
# ā
Solution 3: Use PYTHONPATH
# export PYTHONPATH="/Users/username/modules"Issue 2: Importing Same Module Twice
# ā Problem - module in multiple sys.path locations
import sys
print(sys.path) # May have duplicates
# ā
Solution - keep sys.path clean
if "/path/to/module" not in sys.path:
sys.path.append("/path/to/module")import sys
import os
def debug_import_path(module_name):
"""Help understand how Python finds modules."""
print(f"\nSearching for: {module_name}")
print("\nSearching in paths:")
for i, path in enumerate(sys.path):
print(f" {i}: {path}")
module_file = os.path.join(path, f"{module_name}.py")
if os.path.exists(module_file):
print(f" ā Found at {module_file}")
return module_file
print(f" ā Not found!")
return None
# Usage
debug_import_path("my_module")Ready to practice? Challenges | Quiz
Resources
Ojasa Mirai
Master AI-powered development skills through structured learning, real projects, and verified credentials. Whether you're upskilling your team or launching your career, we deliver the skills companies actually need.
Learn Deep ⢠Build Real ⢠Verify Skills ⢠Launch Forward