Goal
You’ll be able to import and use Python modules correctly, and understand the practical difference between the several import syntax options, not just their surface syntax.
Learn
A module is a file of reusable Python code. Python’s standard library includes many built-in modules covering common needs:
import math math.sqrt(16) # 4.0 math.pi # 3.14159...
There are several ways to import, each with genuinely different practical implications:
import math # access via math.sqrt() from math import sqrt # access via sqrt() directly, no prefix from math import sqrt as sq # access via sq(), a custom alias from math import * # imports everything — generally discouraged
import math keeps everything namespaced under math., making it completely clear in your code where a function actually came from. from math import sqrt is more convenient to type, but if you have your own function also named sqrt, this creates a genuine naming collision, and it becomes less obvious at a glance which module a given function belongs to when reading unfamiliar code later.
from math import * is generally discouraged in real projects specifically because it imports everything from that module into your current namespace at once, making naming collisions much more likely and making it genuinely hard to tell, just by reading the code, where any given function actually came from.
You can also import your own code from other files you’ve written, using the filename (without .py) as the module name — this is how larger programs get organized into multiple files instead of one enormous single file.
Decision Task
You’re working on a large project with several imports, and you notice a function called process() behaving unexpectedly. Before reading on: would you rather this project have used from utils import * or import utils (calling utils.process()), if you’re trying to quickly find out exactly where process() actually came from?
Show Answer
import utils, calling utils.process() — this makes it immediately, explicitly clear in the code itself that process() comes from the utils module. With from utils import *, process() could have come from utils, or from any of several other star-imported modules, or even collided with a locally defined function of the same name, making the actual source genuinely ambiguous just from reading the code.
Common Mistake
Using from module import * out of convenience in real project code, rather than just quick, throwaway scripts. This makes naming collisions more likely and makes it genuinely harder for anyone reading the code later (including yourself, months from now) to determine where a given function actually came from, since it isn’t namespaced or explicitly imported by name.
Practice Questions
1. Write an import statement that lets you call sqrt() directly, without a math. prefix.
Show Answer
from math import sqrt
2. Why is import math generally considered more transparent/readable than from math import *, especially in larger projects?
Show Answer
It keeps every usage explicitly namespaced (math.sqrt()), making it immediately clear where a given function came from when reading the code, unlike a star import which pulls everything in with no namespace prefix at all.
3. Write an import statement that imports sqrt from math, giving it the custom alias “root”.
Show Answer
from math import sqrt as root
4. True or False: from math import * is generally recommended for real production code because it’s more concise.
Show Answer
False — it’s generally discouraged in real projects specifically because it increases the risk of naming collisions and reduces code readability/traceability.
5. What does math.pi represent, and how would you access it using the from math import * style instead?
Show Answer
The mathematical constant pi (~3.14159); with from math import *, you’d access it as just pi, without the math. prefix.
Try It Yourself
Without looking back, write code that imports the math module properly (not using *), then uses it to calculate and print the square root of 81.
Show Answer
import math\nprint(math.sqrt(81)) — outputs 9.0, keeping the usage clearly namespaced under math.
Quick Check
1. What is a module in Python?
Show Answer
A file of reusable Python code.
2. What does import math give you access to, syntactically?
Show Answer
Everything in the math module, accessed with a math. prefix, like math.sqrt().
3. What’s the practical downside of from module import *?
Show Answer
It increases the risk of naming collisions and makes it harder to tell where a given function actually came from when reading the code.
4. How do you give an imported function a custom alias?
Show Answer
Using the as keyword, e.g. from math import sqrt as sq.
5. Can you import your own Python files as modules, not just built-in ones?
Show Answer
Yes — using the filename (without .py) as the module name.