Packages, and installing more with pip
🌍 In real life. A library doesn't put every book on one shelf: it has sections (science, stories), and shelves inside each section. A big collection of Python modules is organised the same way, as folders of modules called packages. And beyond the library in your town, there's a giant shared library of packages written by people all over the world.
The idea in one sentence: a package is a folder of modules (with an __init__.py file), reached with dots like from shapes.square import area, and pip installs packages other people have published.
Its words:
- package: a folder of modules;
__init__.pymarks it as one - dotted import:
shapes.squaremeans "the square module inside the shapes package" - PyPI: the Python Package Index, where published packages live
pip install name: downloads and installs a package on your own computer- virtual environment: a private folder of packages for one project
Step by step, for from shapes.square import area:
- Python finds the folder
shapesand runs its__init__.py. - Inside it, it finds
square.pyand runs it. - It puts the name
areafrom that module on your desk.
from shapes import circle, square
print(circle.area(1))
print(square.area(3))from shapes.square import area
print(area(5))Millions of packages written by other people live on PyPI, the Python Package Index. On your own computer you install one with pip in a terminal, for example pip install requests. This app runs Python in your browser, so here you can use the standard library and your own files, but not installed packages:
import requestsTo explore a module, dir(module) lists the names inside it.
import math
names = [n for n in dir(math) if not n.startswith("_")]
print(len(names) > 40)
print(names[:6])Virtual environments: a separate set of packages for each project. On your own computer, pip install puts packages in one shared place, so two projects that need different versions of a package clash. A virtual environment is a private folder of packages for one project:
python -m venv .venvmakes it, inside the project folder- activate it:
.venv\Scripts\activateon Windows,source .venv/bin/activateon macOS and Linux - now
pip installputs packages only in.venv pip freeze > requirements.txtwrites down the exact versions, andpip install -r requirements.txtsets up the same packages on another computer
These are terminal commands, not Python code, and the Python in this app runs inside the browser without a terminal, so there is no example to run here.
Mistakes and edge cases.
- A package that isn't installed.
import requestsgivesModuleNotFoundError: No module named 'requests'(above). On your own computer,pip install requestsfixes it; in this app, only the standard library and your own files are available. - Typing pip into Python.
pip install ...is a terminal command, not Python code; inside a program it's a SyntaxError. - Installing into the wrong place. Without an active virtual environment,
pipinstalls for everyone on the computer, and two projects can clash. - Forgetting requirements.txt. Without it, someone else can't install the exact versions your project needs.
Quiz
What turns a folder into a package?
- A file called __init__.py inside it
- Naming the folder package
- Running pip on it
How do you import the function
areafrom the modulesquarein the packageshapes?- from shapes.square import area
- import area from shapes
- from shapes/square import area
What is pip for?
- Installing packages from PyPI on your computer
- Printing to the screen
- Making a folder into a package
What does this print?
import math print("sqrt" in dir(math))Which line stops this program with an error?
import math print(math.sqrt(16)) print(floor(2.7))
Coding tasks
The package
shophas a modulepriceswithprice_of(item). Read an item name and print its price.Now
shopalso hastax.add_tax(amount). Writebill(items)that returns the total ofadd_tax(price_of(item))for every item in the list.