Learn Python / Modules and Imports

Modules and Imports

A module is a file of Python code that you can reuse in other programs. Python comes with a huge standard library of modules for maths, dates, random numbers, files, the web and much more. Using them is often the fastest way to get something done.

import

import module_name loads a module. You then use its contents with a dot:

import math

print(math.sqrt(81))
print(math.pi)
print(math.factorial(5))
Output

Imports normally go at the top of a file, so it's easy to see what a program depends on.

from … import

To use a few names without the module prefix, import them directly:

from math import sqrt, pi

print(sqrt(16))
print(round(pi, 3))
Output

Avoid from module import *. It pulls in every name and makes it hard to tell where things come from, and it can silently overwrite your own variables.

Aliases with as

Give a module or a name a shorter alias with as. Some aliases are so common they're a convention, like import numpy as np:

import statistics as stats
from random import randint as roll

scores = [72, 85, 90, 66, 98]
print(stats.mean(scores))
print(stats.median(scores))
print(roll(1, 6))
Output

A tour of useful modules

random

import random

print(random.randint(1, 10))          # whole number from 1 to 10
print(random.random())                # float between 0 and 1
print(random.choice(["rock", "paper", "scissors"]))

cards = ["A", "K", "Q", "J", "10"]
random.shuffle(cards)                 # shuffles in place
print(cards)
print(random.sample(range(1, 50), 6)) # 6 unique lottery numbers
Output

Run it a few times: you get different results each time. To get the same "random" sequence every time, which is useful for testing, set a seed with random.seed(42).

statistics

import statistics

temps = [21.5, 19.0, 24.3, 22.8, 18.1, 22.8]

print(statistics.mean(temps))
print(statistics.median(temps))
print(statistics.mode(temps))
print(round(statistics.stdev(temps), 2))
Output

collections

Counter counts things for you, and defaultdict creates missing keys automatically:

from collections import Counter, defaultdict

words = "the cat and the dog and the bird".split()
counts = Counter(words)
print(counts)
print(counts.most_common(2))

groups = defaultdict(list)
for word in words:
    groups[len(word)].append(word)
print(dict(groups))
Output

string, time and more

import string
import time

print(string.ascii_lowercase)
print(string.digits)

start = time.time()
total = sum(range(1_000_000))
print(f"Took {time.time() - start:.3f} seconds")
Output

Exploring a module

dir() lists what a module contains, and help() shows its documentation:

import random

names = [n for n in dir(random) if not n.startswith("_")]
print(names)
Output

Creating your own module

Any .py file is a module. Normally you'd create a second file in your editor. Here we'll write one from code, then import it:

code = '''
def greet(name):
    return f"Hello, {name}!"

PI_ROUGH = 3.14
'''

with open("helpers.py", "w") as f:
    f.write(code)

import importlib
import helpers
importlib.reload(helpers)   # pick up your edits when you run this again

print(helpers.greet("Ada"))
print(helpers.PI_ROUGH)
Output

You'll learn about open() and with in the next lesson.

Python imports a module only once per session and then reuses it. That's why the example calls importlib.reload(): without it, editing code and running again would still show the old version. In normal programs you rarely need this.

The if __name__ == "__main__" check

When a file is run directly, Python sets its special variable __name__ to "__main__". When it's imported, __name__ is the module's name instead. That lets a file contain test code that runs only when it's run directly:

print("This file's __name__ is:", __name__)

def main():
    print("Running as a program")

if __name__ == "__main__":
    main()
Output

Installing third-party packages

Beyond the standard library, thousands of packages are available from PyPI, the Python Package Index. On your own computer you install them with pip install package-name. In this browser environment, many popular packages such as numpy load automatically the first time you import them:

import numpy as np

data = np.array([1, 2, 3, 4, 5])
print(data * 10)
print(data.mean())
Output

The first run takes a few seconds while the package downloads.

Exercises

Exercise 1: Dice roller

Use the random module to roll two six-sided dice and print both values and their total.

# roll two dice
Output

Exercise 2: Circle maths

Import only pi and sqrt from math. Print the area of a circle with radius 3 (to 2 decimals), and the radius of a circle whose area is 50 (to 2 decimals). It should print 28.27 and 3.99.

# import pi and sqrt from math
Output

Exercise 3: Most common letters

Use collections.Counter to find the three most common letters in the sentence, ignoring spaces.

sentence = "the quick brown fox jumps over the lazy dog"
Output

Exercise 4: Random password

Use random.choice and the string module to build an 8-character password from letters and digits.

import random
import string
Output

Useful module functions at a glance

A quick reference for later. Results marked "e.g." are random and change on every run. The statistics examples use data = [2, 4, 4, 5, 7].

Function What it does Example Result
random.randint(a, b) A random whole number from a to b, both included random.randint(1, 6) e.g. 4
random.random() A random float from 0 up to (not including) 1 random.random() e.g. 0.7134
random.choice(items) One random item random.choice(["rock", "paper", "scissors"]) e.g. 'paper'
random.sample(items, k) k different random items random.sample(range(1, 50), 3) e.g. [7, 31, 12]
random.shuffle(items) Shuffles a list in place random.shuffle(cards) cards in a new order
random.seed(n) Makes the "random" results repeatable random.seed(42) the same results every run
statistics.mean(data) The average statistics.mean(data) 4.4
statistics.median(data) The middle value statistics.median(data) 4
statistics.mode(data) The most common value statistics.mode(data) 4
statistics.stdev(data) How spread out the values are round(statistics.stdev(data), 2) 1.82
Counter(items) Counts each item Counter("banana").most_common(1) [('a', 3)]
defaultdict(list) A dictionary that creates missing keys automatically defaultdict(list)["new"] []
string.ascii_lowercase The letters a to z string.ascii_lowercase[:5] 'abcde'
string.digits The characters 0 to 9 string.digits '0123456789'
time.time() Seconds since 1 January 1970; subtract two of them to time code time.time() e.g. 1791234567.89
time.sleep(seconds) Pauses the program time.sleep(0.5) waits half a second
dir(module) Lists everything in a module dir(math) a list of names, such as 'pi' and 'sqrt'

Summary

  • A module is a reusable file of Python code. The standard library ships with Python.
  • import module gives you module.name, and from module import name gives you name directly.
  • as creates an alias: import statistics as stats.
  • Handy modules include math, random, statistics, collections, string and time.
  • Any .py file is a module. Use if __name__ == "__main__": for code that should run only when the file runs directly.
  • Third-party packages come from PyPI and are installed with pip.