Python 3D Academyবাংলা

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__.py marks it as one
  • dotted import: shapes.square means "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:

  1. Python finds the folder shapes and runs its __init__.py.
  2. Inside it, it finds square.py and runs it.
  3. It puts the name area from 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 requests

To 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:

  1. python -m venv .venv makes it, inside the project folder
  2. activate it: .venv\Scripts\activate on Windows, source .venv/bin/activate on macOS and Linux
  3. now pip install puts packages only in .venv
  4. pip freeze > requirements.txt writes down the exact versions, and pip install -r requirements.txt sets 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 requests gives ModuleNotFoundError: No module named 'requests' (above). On your own computer, pip install requests fixes 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, pip installs 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

  1. What turns a folder into a package?

    • A file called __init__.py inside it
    • Naming the folder package
    • Running pip on it
  2. How do you import the function area from the module square in the package shapes?

    • from shapes.square import area
    • import area from shapes
    • from shapes/square import area
  3. What is pip for?

    • Installing packages from PyPI on your computer
    • Printing to the screen
    • Making a folder into a package
  4. What does this print?

    import math
    print("sqrt" in dir(math))
  5. Which line stops this program with an error?

    import math
    print(math.sqrt(16))
    print(floor(2.7))

Coding tasks

  1. The package shop has a module prices with price_of(item). Read an item name and print its price.

  2. Now shop also has tax.add_tax(amount). Write bill(items) that returns the total of add_tax(price_of(item)) for every item in the list.