Learn / Programming / Python / Modules, Packages and pip

Python · Lesson 13 of 15

Modules, Packages and pip

Split code across files, import the standard library, and install packages in a virtual environment.

  • Intermediate
  • 13 min read
  • 3 objectives

Before this lessonLesson 12: Error Handling

What you will learn

  • Import from your own modules
  • Use the standard library
  • Create a venv and install packages

Once a program grows past one file you need a way to split it up and to reuse code written by others. A module is simply a .py file. A package is a folder of modules. Python ships with a large standard library, and the community publishes hundreds of thousands more packages you can install with pip.

Importing from the standard library

The import statement loads a module. Access its contents with a dot, or pull out specific names with from ... import. Add as to give something a shorter name.

import math
from random import Random
from collections import Counter as C

print(math.sqrt(49), math.pi)
print(math.ceil(4.1), math.floor(4.9))

rng = Random(7)          # seeded so the result is repeatable
print(rng.randint(1, 100))

print(C("banana").most_common(2))
Output
7.0 3.141592653589793
5 4
42
[('a', 3), ('n', 2)]

Writing your own module

Any file you create can be imported by the others in the same folder. Suppose you save helper functions in shapes.py:

# shapes.py
PI = 3.14159

def circle_area(r):
    return PI * r ** 2

def rect_area(w, h):
    return w * h

Now another file in the same folder can use them:

# main.py
import shapes
from shapes import rect_area

print(shapes.circle_area(2))
print(rect_area(3, 4))

The __name__ guard

When you run a file directly, Python sets its __name__ to "__main__". When the file is imported, __name__ is the module name instead. Wrapping your script in if __name__ == "__main__": means the code runs when you execute the file but not when another file imports it.

def main():
    print("running as a script")

if __name__ == "__main__":
    main()

Packages: folders of modules

Put modules in a folder to make a package. An __init__.py file (often empty) marks the folder as a package, and you import with dots.

myapp/
  __init__.py
  main.py
  utils/
    __init__.py
    text.py      # from myapp.utils.text import slugify

Virtual environments and pip

A virtual environment is a private folder of installed packages for one project, so different projects can use different versions without clashing. Create one, activate it, then install with pip. Record the exact versions in requirements.txt so anyone can recreate the setup.

python3 -m venv .venv
source .venv/bin/activate        # Windows: .venv\Scripts\activate

pip install requests
pip freeze > requirements.txt
pip install -r requirements.txt   # on another machine
import requests

response = requests.get("https://api.github.com")
print(response.status_code)

Common mistakes

  • Naming your file like a library: a file called random.py or math.py shadows the real module and breaks imports.
  • Circular imports: module A imports B while B imports A. Move the shared code into a third module.
  • Wildcard imports: from x import * hides where names come from. Import what you need by name.
  • Committing the venv: add .venv/ to .gitignore and share requirements.txt instead.
# Write your solution here
Up next · Lesson 14JSON, Dates and the Standard LibraryRead and write JSON, work with dates and durations, and reach for collections and pathlib.