Packages and Import Statements
Learn how Python organizes reusable code into packages, and how to import it correctly.
As Python projects grow, keeping code in a single file becomes unmanageable. Packages solve this by letting you group related modules into folders, while the import statement is how you pull that code into the file you're working in.
Modules vs Packages
A module is simply a single .py file. A package is a folder containing multiple modules, along with an __init__.py file that tells Python to treat the folder as an importable package.
my_project/
analysis/
__init__.py
cleaning.py
visualization.py
main.py
Different Ways to Import
Python gives you several import styles depending on how much of a module you need:
import pandas import pandas as pd from pandas import DataFrame from analysis.cleaning import remove_nulls
as) for long or frequently used module names — it's why almost every data scientist writes import pandas as pd.Installing Third-Party Packages
Not every package comes pre-installed. Tools like pip let you add external packages such as pandas, matplotlib, or scikit-learn to your environment with a single command: pip install pandas.
Common Import Mistakes to Avoid
- Circular imports — two modules importing each other
- Using wildcard imports (
from module import *) which pollute the namespace - Forgetting
__init__.pyin older Python versions when creating packages
Once imports feel natural, you're ready to structure real, multi-file data science projects with confidence.