Python course menu
Python Packages and pip: Organise Code and Install Libraries
Learn Python packages: organise modules into folders with __init__.py, import from packages, install third-party libraries with pip, use virtual environments (venv), and record dependencies in requirements.txt.
On this page
The word package means two related things in Python:
- A folder of modules that you organise in your own project.
- A library published by someone else (on PyPI, the Python Package Index) that you install with
pip.
This lesson covers both, plus virtual environments, which keep each project's libraries separate.
Part 1: Your own packages#
As a project grows, one folder full of .py files gets messy. A package groups related modules into a sub-folder:
project/
├── main.py
└── shop/
├── __init__.py
├── pricing.py
└── inventory.pydef add_tax(price, rate=0.18):
return round(price * (1 + rate), 2)STOCK = {"pen": 120, "notebook": 40}
def in_stock(item):
return STOCK.get(item, 0) > 0from .pricing import add_taxImport with dots, from main.py:
from shop import pricing
from shop.inventory import in_stock
import shop
print(pricing.add_tax(200))
print(in_stock("pen"), in_stock("chair"))
print(shop.add_tax(50))236.0
True False
59.0What is init.py for?#
__init__.py marks the folder as a regular package and runs when the package is first imported. It can be empty. Here it re-exports add_tax, so users can write shop.add_tax without knowing it lives in pricing.py. The . in from .pricing import add_tax means "from this same package" (a relative import).
Part 2: Installing libraries with pip#
Thousands of free libraries are published on PyPI: requests for web requests, pandas for data analysis, flask for web apps. Install them with pip:
python -m pip install requestsUseful pip commands:
You can check whether a package is installed, and which version, from Python itself:
import importlib.metadata as md
try:
print(md.version("requests"))
except md.PackageNotFoundError:
print("requests is not installed")2.32.5Your version number will depend on what is installed on your computer.
Part 3: Virtual environments#
If one project needs requests 2.28 and another needs 2.32, installing both globally causes conflicts. A virtual environment is a private folder of packages for one project.
# 1. Create it (once per project), inside the project folder
python -m venv .venv
# 2. Activate it
# Windows (PowerShell):
.venv\Scripts\Activate.ps1
# macOS / Linux:
source .venv/bin/activate
# 3. Install packages: they go into .venv only
python -m pip install requests
# 4. Leave it when done
deactivateWhile it is active, your terminal prompt usually shows (.venv). Editors like VS Code detect .venv automatically.
requirements.txt#
Record a project's dependencies so anyone can recreate them:
python -m pip freeze > requirements.txtThe file lists every package in the environment with its exact version, one per line, for example requests==2.32.5. It also includes the libraries requests itself depends on (such as urllib3 and certifi), because pip freeze records everything installed. This is another reason to work in a virtual environment: the file then contains only what this project uses.
Someone else (or you, on another computer) runs:
python -m pip install -r requirements.txtCommon mistakes#
Practice#
Practice exercise
Create a package called textutils with a module cleaning.py containing a function normalize(text) that strips spaces and lowercases text. Make from textutils import normalize work, then call it on " Hello World ".
Show hint
You need textutils/__init__.py containing from .cleaning import normalize.
Show solution
project/
├── main.py
└── textutils/
├── __init__.py
└── cleaning.pydef normalize(text):
return text.strip().lower()from .cleaning import normalizefrom textutils import normalize
print(repr(normalize(" Hello World ")))'hello world'Key takeaway#
Finished this lesson?