Packages and Import Statements
Estimated study time: 10 minutes.
As projects grow, keeping all your code in a single file becomes unmanageable. Packages solve this by grouping related modules together, and import statements let you pull specific pieces of code into the file where you need them.
What is a Package?
A package is simply a folder containing related modules (files of code), often with an initializer file that tells the language this folder should be treated as an importable unit. Packages can be your own, or installed from a public registry.
Import Statement Basics
# Import an entire module
import pandas
# Import a specific function from a module
from pandas import read_csv
# Import with an alias
import numpy as np
Why This Matters
- Keeps large codebases organized into logical units
- Encourages code reuse instead of duplication
- Makes it easy to bring in third-party functionality
- Improves readability — you can see exactly what a file depends on
💡 Tip: Avoid wildcard imports like
from module import * in real projects — they make it unclear where a function actually came from.