Python Import Statement
Table of Contents + −
In the last lesson you learned about Python Modules. A module is just a file full of code you can reuse. Now the question is simple. How do you actually pull that code into your own file? That is the job of the import statement. There is more than one way to import. Picking the right one keeps your code clean and easy to read.
🤔 Why Different Import Styles Exist
Say you want to use a ready-made tool. The math module that comes with Python is a good example. Here is the pain. If you type the wrong kind of import, your names can clash. They can get too long to type. Or you lose track of where they came from.
So Python gives you a few import styles. Each one fits a slightly different situation. Once you know what each one does, you just reach for the right one and keep going.
Here are the styles we will look at:
import module— bring in the whole moduleimport module as alias— bring it in under a shorter namefrom module import name— bring in just one thingfrom module import *— bring in everything (the risky one)
📦 import module
This is the plain, everyday import. You bring in the whole module. Then you reach into it with a dot whenever you need something.
Here we import the math module and use its sqrt function to get a square root.
import math
result = math.sqrt(16)print(result)Output
4.0Look at how we call it. We write math.sqrt. We say the module name first, then a dot, then the thing we want. That dot is doing real work. It tells anyone reading the code that sqrt comes from math, and not from somewhere else.
This style is the safest default. The module name stays in front of everything. So there is never any confusion about where a name came from.
Tip
Think of import math like opening a toolbox named “math”. Every time you need a tool, you say which box you are reaching into. You write math.sqrt, math.pi, and so on. Nothing gets mixed up.
🏷️ import module as alias
Sometimes the real module name is long. Or the community has agreed on a short name for it. So you give it a nickname when you import it. That nickname is called an alias.
Here we import math but call it m for short.
import math as m
print(m.pi)print(m.sqrt(81))Output
3.1415926535897939.0After import math as m, the name math is gone in this file. You now use m everywhere instead. Same module, shorter name.
You will see this a lot with data libraries. People write import numpy as np and import pandas as pd so often that np and pd are basically standard. When you read someone else’s data code, np.array(...) instantly tells you it is NumPy.
Caution
Pick alias names that people recognize, like np for NumPy. Do not invent a random short name like import math as x. A confusing alias is worse than typing the full name.
🎯 from module import name
What if you only need one function from a big module? And you do not want to type the module name every single time? Then you pull that one name straight in.
Here we grab just sqrt from math, so we can call it on its own.
from math import sqrt
print(sqrt(49))Output
7.0Notice there is no math. in front now. We imported sqrt directly. So we call it as plain sqrt. You can pull in more than one thing too. Just separate them with commas.
This example brings in both pi and sqrt in one line.
from math import pi, sqrt
print(pi)print(sqrt(100))Output
3.14159265358979310.0This style reads nicely when you use the same one or two functions over and over. The trade-off is that the module name is no longer in front. So a reader has to look back at the import line to remember where sqrt came from.
⚠️ from module import * (the risky one)
This last style says one thing. Bring in everything from the module, with no module name in front of any of it.
Here we import everything from math and use a few names directly.
from math import *
print(pi)print(sqrt(64))print(floor(3.9))Output
3.1415926535897938.03It looks convenient, right? No dots. No typing the module name. But this is the one style most teams tell you to avoid. Here is the problem with wildcard imports:
- You cannot tell where any name came from. Reading
sqrt(64)later, you have no clue it is frommath. - It can quietly overwrite your own names. If you have a variable called
piand then runfrom math import *, yourpiis gone. - Two wildcard imports can clash. If two modules both have a
loadfunction, the second import silently wins and you may not notice.
This example shows the silent clash problem in action.
gamma = "my own value"
from math import *
# ❌ Avoid: the import quietly replaced your variableprint(gamma)Output
<built-in function gamma>See what happened? You set gamma to your own text. But math also has a gamma function. And from math import * quietly took over the name. Your text is gone. Printing gamma now shows a math function instead of your value. And you got no warning at all. That is exactly why this style is risky.
🧭 When to Use Which
Here is a quick guide to pick the right style without overthinking it.
| Style | When to use it |
|---|---|
import module | Your safe default. The module name stays in front, so nothing is confusing. |
import module as alias | When there is a well-known short name, like np for NumPy or pd for pandas. |
from module import name | When you use one or two specific names a lot and want them short. |
from module import * | Almost never. Skip it in real code. |
⚠️ Common Mistakes
A few things trip people up when they start importing. Watch for these.
- Forgetting the dot after a plain import. With
import math, you must writemath.sqrt, not justsqrt.
import math
# ❌ Avoid: sqrt does not exist on its own here# print(sqrt(16)) # NameError
# ✅ Good: reach into the module with a dotprint(math.sqrt(16))- Reaching for the full name after an alias. Once you write
import math as m, the namemathno longer works in that file.
import math as m
# ❌ Avoid: you renamed it, so math is gone# print(math.pi) # NameError
# ✅ Good: use the alias you choseprint(m.pi)- Using
from module import *to save typing. It saves a few keystrokes now and costs you hours of confusion later when a name clashes.
✅ Best Practices
Keep these habits and your imports stay clean and clear.
- Reach for plain
import modulefirst. It is the clearest and almost never wrong. - Use an alias only when it is a recognized one, like
nporpd. - With
from module import name, import just the few names you actually use, not a long list. - Put all your import lines at the top of the file, so anyone can see your dependencies at a glance.
- Never use
from module import *in code you plan to keep.
🧩 What You’ve Learned
A quick recap of the import styles and when each one fits.
- ✅
import modulebrings in the whole module, and you use it with a dot likemath.sqrt. - ✅
import module as aliasgives the module a shorter name, likeimport numpy as np. - ✅
from module import namepulls one thing in directly, so you call it without the module name in front. - ✅
from module import *brings in everything and is risky, because it hides where names came from and can overwrite your own names. - ✅ Plain
import moduleis the safe default, and wildcard imports should be avoided in real code.
Check Your Knowledge
Test what you learned. Pick an answer for each question, then click Check.
- 1
After `import math`, how do you call the sqrt function?
Why: With a plain import you reach into the module with a dot, so it is math.sqrt(16).
- 2
What does `import numpy as np` do?
Why: The 'as' keyword gives the module a shorter alias, np, that you use only in this file.
- 3
After `from math import sqrt`, how do you call it?
Why: Importing the name directly means you call it as plain sqrt(49), with no module name in front.
- 4
Why is `from module import *` discouraged?
Why: Wildcard imports bring in everything with no module name, so names can clash and silently overwrite each other.
🚀 What’s Next?
Now you know how to bring other people’s code into your file. Next you will flip it around and build your own modules so other files can import from you.