All lessons
📦

Python Modules

Import and use modules

Reward
+40 XP

Importing modules

Use import to bring in built-in or third-party modules.

import math
print(math.pi)
print(math.sqrt(16))

from ... import

Import specific items from a module.

from math import pi, sqrt
print(pi)
print(sqrt(25))

Aliases

Use as to give a module a shorter name.

import math as m
print(m.pi)

Try it yourself

Run the example below. Edit it and see what changes.

main.py
Output
Press Run to execute (Ctrl/Cmd+Enter)

Common mistakes

  • Naming your file the same as a module (e.g., math.py) — it shadows the built-in.
  • Importing everything with from x import * can cause name conflicts.