Import

The statement that loads a module or name so you can use it in your file.

import brings code from another module into your file. import math loads the whole module (use it as math.sqrt); from math import sqrt pulls one name in directly; import numpy as np gives it a shorter alias. Imports usually go at the top of a file.

Example
import math
from datetime import date
import statistics as stats

print(math.sqrt(9))
print(date.today().year > 2000)
print(stats.mean([2, 4, 6]))
Output
3.0
True
4

Where this shows up in real Python

Every script beyond a few lines starts with imports — the standard library and the installed packages you build on.

Commonly used Import tools

  • import module — load a whole module
  • from module import name — import one name
  • import module as alias — import under a shorter name
  • from module import * — import everything (usually avoid)

Related lessons

Related terms