Importing
Modules in Python
Learn how to import modules and functions using import and from statements to unlock Python's powerful standard library and beyond.
What is a Module?
A module is simply a Python file (ending in .py) that contains definitions: functions, variables, and classes. Modules let you organize code into reusable pieces instead of writing everything in one giant file.
Why Import Modules?
The Module Ecosystem
Your script can pull from three sources: Python's built-in standard library, your own .py files, or third-party packages installed via pip.
The import Statement
result = math.sqrt(144)
print(result) # 12.0
The from...import Statement
result = sqrt(144) # No prefix needed!
print(pi) # 3.141592653589793
This is ideal when you only need a few specific functions and want shorter, cleaner code.
import vs from...import
| import math | from math import sqrt | |
|---|---|---|
| What's loaded | Entire module | Only specified names |
| Usage syntax | math.sqrt() | sqrt() |
| Name collisions | Protected by prefix | Possible if names overlap |
| Readability | Clear origin of functions | Shorter, cleaner calls |
| Best for | Using many functions | Using a few functions |
Aliasing with as
print(m.sqrt(64)) # 8.0
print(m.pi) # 3.14159...
print(fact(5)) # 120
The Wildcard Import: *
print(sqrt(25)) # 5.0
print(cos(0)) # 1.0
print(pi) # 3.14159...
The wildcard import is acceptable for quick experiments in the interactive shell, but should be avoided in scripts and production code.
How Importing Works
Step-by-Step: How to Import
Example 1: The random Module
# Random integer between 1 and 6
dice = random.randint(1, 6)
print("You rolled:", dice)
# Pick a random item from a list
colors = ["red", "blue", "green"]
pick = random.choice(colors)
print("Random color:", pick)
Example 2: from datetime import
today = date.today()
print("Today:", today)
next_week = today + timedelta(days=7)
print("Next week:", next_week)
Example 3: Combining Techniques
from math import ceil, floor
import json as j
# Full import: prefix required
print(os.getcwd())
# Selective import: no prefix
print(ceil(4.2)) # 5
print(floor(4.8)) # 4
# Aliased import: use alias
data = j.dumps({"name": "Alice"})
Common Mistakes to Avoid
Import Syntax Summary
| Syntax | Usage | Example Call |
|---|---|---|
| import math | Full module | math.sqrt(9) |
| from math import sqrt | Specific name | sqrt(9) |
| import math as m | Aliased module | m.sqrt(9) |
| from math import sqrt as s | Aliased name | s(9) |
| from math import * | Everything (avoid!) | sqrt(9) |
Question 1 of 5
After writing import random, which of the following correctly generates a random integer between 1 and 10?
Question 2 of 5
What does the following code do?
from os import getcwd
Question 3 of 5
Which statement correctly creates an alias np for the module numpy?
Question 4 of 5
Why is from math import * generally discouraged in production code?
Question 5 of 5
You have a file called helpers.py with a function greet(). Which import lets you call greet() directly without a prefix?