Modules, Packages & pip
A module is a Python file full of code you can reuse. You bring it into your program with import. Python comes with a huge standard library of ready-made modules:
import math # bring in the math module
print(math.sqrt(16)) # 4.0
print(math.pi) # 3.141592653589793
You can also install thousands of extra packages written by others using pip. Let's see how it all fits together.
Importing modules
There are a few import styles. Pick the one that reads best:
# Import the whole module, use module.name
import random
print(random.randint(1, 6)) # a dice roll: 1 to 6
# Import specific names directly
from math import sqrt, pi
print(sqrt(25)) # 5.0 (no math. prefix needed)
# Give a module a shorter alias
import datetime as dt
print(dt.date.today()) # today's date
Useful standard library modules
The standard library ships with Python — no installation needed. A few you'll meet early:
import random
print(random.choice(["heads", "tails"])) # random pick
import math
print(math.floor(3.7), math.ceil(3.2)) # 3 4
import datetime
now = datetime.datetime.now()
print(now.year) # current year
Writing your own module
Any .py file you write is a module you can import. Say you have a file helpers.py:
# helpers.py
def greet(name):
return f"Hello, {name}!"
PI = 3.14159
In another file in the same folder, import and use it:
# main.py
import helpers
print(helpers.greet("Asha")) # Hello, Asha!
print(helpers.PI) # 3.14159
A package is just a folder of related modules. As programs grow, packages keep code organised.
Want to learn this properly?
Join the waitlist for our courses — beginner-friendly, project-first classes in Jalgaon.
Browse coursesInstalling packages with pip
For tools outside the standard library — like requests for web requests — use pip, Python's package installer. Run these commands in your terminal, not inside a Python file:
# Install a package
pip install requests
# On Mac/Linux you may need pip3
pip3 install requests
# See what's installed
pip list
# Install a specific version
pip install requests==2.31.0
Then use it in your code:
import requests
response = requests.get("https://api.github.com")
print(response.status_code) # 200 if it worked
Virtual environments (highly recommended)
A virtual environment keeps each project's packages separate, so projects don't clash. Create and activate one before installing packages:
# Create a virtual environment named .venv
python -m venv .venv
# Activate it
# On Windows:
.venv\Scripts\activate
# On Mac/Linux:
source .venv/bin/activate
# Now pip install affects only this project
pip install requests
When you're done, type deactivate. Using a virtual environment per project is a habit worth building early.
Common mistakes
- Running pip inside Python:
pip install requestsgoes in the terminal, not in a.pyfile or the Python shell. ModuleNotFoundError: You tried to import something you haven't installed, or you installed it in a different environment. Check withpip listand your active virtual environment.- Naming your file like a module: Saving a file as
random.pyormath.pyshadows the real module and causes confusing errors. Avoid those names. pipvspip3: On Mac/Linux,pipmay point to an old Python. Usepip3, or better,python -m pip install ...to be sure you're using the right one.- Forgetting to activate the virtual environment: If imports work in one terminal but not another, you probably forgot to activate the venv.
FAQ
What's the difference between a module and a package?
A module is a single .py file. A package is a folder containing multiple modules (with the files organised together).
Where does pip download packages from? From the Python Package Index (PyPI) at pypi.org — a public repository of community and official packages.
Do I need a virtual environment for small scripts? Not strictly, but it's a great habit. It prevents version conflicts once you work on more than one project.
Modules unlock powerful tools like the ones used in Regular Expressions in Python and Web Scraping Basics with Python. Build on your knowledge from Functions in Python too. More on the Python learning hub.
Want to learn this properly? Join the waitlist for our Python course — taught in Jalgaon, beginner-friendly.
Want to learn this properly?
Join the waitlist for our courses — beginner-friendly, project-first classes in Jalgaon.
Browse coursesInstructor, Infoplanet
Kedar Kabra teaches Python at Infoplanet, helping beginners become confident programmers through hands-on, project-first practice.
Related guides
Dictionaries in Python
Understand Python dictionaries — storing data as key-value pairs — including adding, accessing, updating, looping, and safely reading values with .get().
Exception Handling in Python
Handle errors gracefully in Python with try, except, else, and finally — so your program responds to problems instead of crashing.
File Handling in Python
Read from and write to files in Python using with open() — covering file modes, reading line by line, appending, and safe file handling.
