Modules and Imports

Python 3.14 · ✓ verified by execution on 2026-07-23

Modules and Imports

A module is a file containing Python definitions and statements. The file name is the module name with the suffix .py appended.

When you have written a handy function, you do not want to copy its definition into every program. Instead, you can save it as a .py file and import it.

Step through the visualizer below to see how the math module is brought into our local namespace.

python · visualize
import math
print(math.sqrt(16.0))

Importing Specific Names

There is a variant of the import statement that imports names from a module directly into the importing module’s namespace.

Instead of typing the module name every time, you can pull specific items straight into your code. Notice how this time, only pi appears in the namespace, not the entire math module!

python · visualize
from math import pi
print(round(pi, 2))

Aliasing Modules

If the module name is followed by as, then the name following as is bound directly to the imported module.

This is extremely common in data science libraries (like import pandas as pd).

python · visualize
import math as m
print(m.sqrt(25.0))

The Standard Library

Python comes with a library of standard modules, described in a separate document, the Python Library Reference

Python includes batteries out of the box! You can access random number generators, math operations, system paths, and more.

python
import random
# mock random for test stability
random.seed(42)
print(random.randint(1, 10))
Output
2

The Module Search Path

The variable sys.path is a list of strings that determines the interpreter’s search path for modules.

python
import sys
print(type(sys.path))
Output
<class 'list'>

Executing Modules as Scripts

the code in the module will be executed, just as if you imported it, but with the name set to “main”.

If you import a module, its __name__ is the module’s name. If you run the file directly, it runs as __main__.

python
if __name__ == '__main__':
    print('Running as main script')
Output
Running as main script

Edge Cases

Check yourself

Importing a module multiple times runs its code multiple times.

Reveal answer

For efficiency reasons, each module is only imported once per interpreter session. — For efficiency reasons, each module is only imported once per interpreter session.

Using `from module import *` is the standard and best way to import.

Reveal answer

Note that in general the practice of importing * from a module or package is frowned upon, since it often causes poorly readable code. — Note that in general the practice of importing * from a module or package is frowned upon, since it often causes poorly readable code.

The __name__ variable is always the file name.

Reveal answer

When a module is executed directly as a script (e.g. `python my_script.py`), its `__name__` is set to the string `'__main__'` instead of the file name. — When a module is executed directly as a script (e.g. `python my_script.py`), its `__name__` is set to the string `'__main__'` instead of the file name.

Variables from an imported module overwrite your own variables automatically.

Reveal answer

Each module has its own private namespace. Unless you use `from module import *`, the module's contents are kept inside its own namespace (e.g. `module.variable`). — Each module has its own private namespace. Unless you use `from module import *`, the module's contents are kept inside its own namespace (e.g. `module.variable`).

Challenges

Challenge 1 +25 XP

Import the `math` module and print the value of `math.pi` rounded to 4 decimal places using the `round()` function.

python
  • Test 1 — expects "3.1416"
Show solution (0 XP)
import math
print(round(math.pi, 4))

Challenge 2 +25 XP

Write an `if` statement that checks if the script is being run directly. If it is, print the string 'Running directly!'.

python
  • Test 1 — expects "Running directly!"
Show solution (0 XP)
if __name__ == '__main__':
    print('Running directly!')