Modules, Packages & Virtual Environments
As your Python programs grow, organizing code into multiple files becomes essential. Python's module system makes this natural. Virtual environments keep your project dependencies clean and isolated.
Why This Chapter Matters
Every real Python project uses modules and manages dependencies. Understanding how to structure a project, import code properly, and isolate your environment is foundational for professional development.
What a Module Is
A module is simply a .py file. Any Python file can be imported into another Python file.
project/
├── main.py
├── helpers.py
└── math_utils.py
helpers.py:
def greet(name):
return f"Hello, {name}!"
AUTHOR = "Nandhoo"
main.py:
import helpers
print(helpers.greet("Asha"))
print(helpers.AUTHOR)
Import Styles
import module
import math
print(math.sqrt(16)) # 4.0
print(math.pi)
from module import name
from math import sqrt, pi
print(sqrt(16)) # 4.0 — no need for math. prefix
from module import * (avoid in production)
from math import * # imports everything — pollutes namespace
import module as alias
import numpy as np
import pandas as pd
arr = np.array([1, 2, 3])
from module import name as alias
from datetime import datetime as dt
now = dt.now()
The __name__ Guard
Every Python file has a built-in __name__ variable.
- When run directly:
__name__ == "__main__" - When imported:
__name__equals the module name
# helpers.py
def greet(name):
return f"Hello, {name}!"
if __name__ == "__main__":
# This block only runs if you execute this file directly
# It does NOT run when the file is imported elsewhere
print(greet("Testing greet"))
This pattern is the standard way to write runnable, importable modules.
Standard Library Modules
Python comes with hundreds of built-in modules. The most commonly used:
| Module | Purpose |
|---|---|
os | OS interactions, file paths, environment variables |
sys | System information, Python path, exit |
math | Mathematical functions |
random | Random numbers, choices, shuffling |
datetime | Dates and times |
collections | Counter, defaultdict, namedtuple, deque |
itertools | Efficient iterators (chain, product, combinations) |
functools | Higher-order functions (lru_cache, partial, reduce) |
re | Regular expressions |
json | JSON encoding and decoding |
csv | CSV reading and writing |
pathlib | Object-oriented file paths |
logging | Logging framework |
time | Time functions and sleep |
threading | Thread-based concurrency |
subprocess | Run system commands |
import random
print(random.randint(1, 100))
print(random.choice(["apple", "banana", "cherry"]))
from collections import Counter
words = ["cat", "dog", "cat", "bird", "dog", "cat"]
print(Counter(words)) # Counter({'cat': 3, 'dog': 2, 'bird': 1})
from datetime import datetime, timedelta
now = datetime.now()
print(now.strftime("%Y-%m-%d %H:%M:%S"))
next_week = now + timedelta(days=7)
Packages
A package is a directory of modules with an __init__.py file.
myapp/
├── __init__.py
├── models/
│ ├── __init__.py
│ ├── user.py
│ └── post.py
└── utils/
├── __init__.py
└── helpers.py
Import from packages:
from myapp.models.user import User
from myapp.utils import helpers
pip — The Python Package Manager
Install third-party packages from PyPI:
pip install requests
pip install numpy pandas matplotlib
pip install flask
Uninstall:
pip uninstall requests
List installed packages:
pip list
pip show requests
Virtual Environments
A virtual environment is an isolated Python environment for a specific project. It keeps project dependencies separate from each other and from system Python.
Why Virtual Environments Matter
Without one:
- Project A needs
requests==2.28 - Project B needs
requests==2.31 - You cannot have both globally — they conflict
With venv, each project has its own isolated copy.
Creating a Virtual Environment
# Create the environment
python -m venv venv
# Activate it (macOS / Linux)
source venv/bin/activate
# Activate it (Windows)
venv\Scripts\activate
Your prompt changes to show (venv) indicating the environment is active.
Using the Virtual Environment
# Now pip installs into the venv, not globally
pip install requests flask
# Check what's installed
pip list
Deactivating
deactivate
Never Commit venv to Git
Add to .gitignore:
venv/
__pycache__/
*.pyc
.env
requirements.txt
Share your project's dependencies with others:
# Save current dependencies to a file
pip freeze > requirements.txt
The file looks like:
requests==2.31.0
flask==3.0.0
numpy==1.26.0
Install all dependencies on another machine:
pip install -r requirements.txt
pyproject.toml (Modern Alternative)
Modern Python projects use pyproject.toml for dependency management with tools like poetry or hatch:
[tool.poetry.dependencies]
python = "^3.11"
fastapi = "^0.100.0"
Commonly Used Third-Party Packages
| Package | Purpose |
|---|---|
requests | HTTP requests |
flask | Lightweight web framework |
fastapi | Modern async web API framework |
django | Full-featured web framework |
numpy | Numerical computing |
pandas | Data analysis and DataFrames |
matplotlib | Data visualization |
sqlalchemy | SQL ORM |
pytest | Testing framework |
pydantic | Data validation |
python-dotenv | Load .env files |
Common Mistakes
- installing packages globally instead of inside a virtual environment
- committing
venv/to version control - forgetting to activate the virtual environment before
pip install - not saving
requirements.txtbefore sharing the project - importing with
from module import *which pollutes the namespace
Mini Exercises
- Create a new directory, initialize a virtual environment, and install
requests. - Write two modules:
greetings.pyandmain.py. Import a function fromgreetings.py. - Add
if __name__ == "__main__":togreetings.pywith a test call. - Run
pip freeze > requirements.txtand inspect the output. - Use the
collections.Counterto count character frequencies in a string.
Review Questions
- What is the difference between
import moduleandfrom module import name? - What does
if __name__ == "__main__":do? - Why is it important to use a virtual environment for each project?
- What is
requirements.txtand how do you create one? - What does
pip install -r requirements.txtdo?
Reference Checklist
- I can import modules using all common import styles
- I understand the
__name__guard and why it is used - I know the most useful standard library modules
- I can create, activate, and deactivate a virtual environment
- I can install packages with
pipand save them torequirements.txt - I know how to structure a project with packages and
__init__.py