Packages and pip
15 min · Beginner
Need help? Ask a tutor on WhatsApp (opens in a new tab)A package is a folder of modules. Python's standard library uses packages, and so do the tools you download. pip is the installer that fetches new packages from the internet.
A package is a folder of modules
Package is a folder of modules, usually with an __init__.py file. You reach a module inside it with dots, such as shop.pricing.
A project with many modules needs order. Put related modules in one folder with a file named __init__.py. That folder is a package, and you reach inside with dots.
shop/ __init__.py pricing.py orders.py# main.py, next to the shop folderfrom shop.pricing import total_with_taximport shop.ordersExample 1 · Counting with a standard library class
from collections import Counterimport json
status = Counter(["open", "closed", "open"])print(status.most_common(1))print(json.dumps({"id": 7, "paid": True}))[('open', 2)]
{"id": 7, "paid": true}What this code does
from collections import Counterpicks one class out of a larger module.most_common(1)gives the top item and its count.json.dumpswrites a dictionary as JSON text. Python'sTruebecomestrue.
shop.pricing means the module pricing inside the package shop. The standard library has packages too: import json is a module, but from os import path and from collections import Counter reach into bigger ones.
Installing with pip
pip is Python's installer for packages from the Python Package Index. Run it as python -m pip install name so it uses the same Python as your code.
The standard library cannot cover everything. Other people publish packages on the Python Package Index. Run pip in the terminal, not inside a Python file.
python -m pip install requestspython -m pip listpython -m pip uninstall requestsExample 2 · Is a package available?
import importlib.util
for name in ["json", "surely_not_installed_pkg"]: found = importlib.util.find_spec(name) is not None print(name, found)json True surely_not_installed_pkg False
What this code does
find_speclooks for a module without loading it. It returnsNonewhen nothing is found.jsonships with Python, so it is found. The made-up name is not.
install downloads a package, list shows what is installed, and uninstall removes it. Writing python -m pip makes sure pip belongs to the same Python that runs your code. Install only packages you trust.
Keep each project separate
Virtual environment is a private folder with its own Python packages for one project. It keeps one project's packages from clashing with another's.
Two projects may need different versions of one package. A virtual environment gives each project its own set of packages, and a requirements.txt file records them.
python -m venv .venvpython -m pip freeze > requirements.txtpython -m pip install -r requirements.txtExample 3 · Which Python runs the code?
import sys
print(sys.version_info.major)print(sys.version_info >= (3, 8))3 True
What this code does
sys.version_infodescribes the running Python..majoris the first number of the version. Comparing with a tuple such as(3, 8)checks a minimum version.
The first line creates an environment in the folder .venv; your editor or terminal then activates it. freeze writes installed names and versions to a file. install -r reads that file, so a teammate can rebuild the same set.
Next step
Check your understanding
9 quick questions