📥 Imports¶
The import system is how Python finds and loads code from other files. Understanding imports is essential for organizing larger projects.
✅ Basic Import Syntax¶
# Import the whole module
import math
print(math.sqrt(16)) # 4.0
# Import specific names
from math import sqrt, pi
print(sqrt(16)) # 4.0
# Import with alias
import numpy as np
from datetime import datetime as dt
✅ What Happens During Import¶
When you import a module, Python:
- Searches for the module in
sys.path - Compiles the
.pyfile to bytecode (cached in__pycache__) - Executes the module code once
- Caches the module in
sys.modules
Subsequent imports of the same module use the cached version.
✅ The Search Path (sys.path)¶
Python looks for modules in this order:
- Current directory (or script directory)
- PYTHONPATH environment variable directories
- Standard library directories
- Site-packages (installed packages)
✅ Import Styles¶
# Style 1: Import module (preferred for large modules)
import os
os.path.join("a", "b") # Clear where join comes from
# Style 2: Import specific names (good for frequently used items)
from os.path import join, exists
join("a", "b") # Shorter, but origin less clear
# Style 3: Import all (generally avoid)
from os.path import * # Pollutes namespace, hard to track origins
✅ Common Import Patterns¶
# Standard library first, then third-party, then local
import os
import sys
import requests
import numpy as np
from myproject import utils
from myproject.models import User
🔍 Key Takeaways¶
import modulevsfrom module import namehave different use cases.- Python caches imports in
sys.modulesfor efficiency. - The search path (
sys.path) determines where Python looks for modules. - Follow PEP 8 import ordering: stdlib, third-party, local.