Lesson 04
from … import in Python — Direct Access and Aliases
Learn 3 from-import syntaxes with a toolbox memory trick: import the whole box vs pick specific tools. Named imports, aliases, and when to avoid import star.
Memory trick:
importbrings the whole toolbox.from … importtakes only the tools you need.
Use the playground and quiz at the bottom to practice all three syntaxes.
What you will learn
- How
from … importdiffers from plainimport - 3 syntax variations for from-import
- Namespace visualization — names copied into your program
- When to alias individual names (not just modules)
- Why
import *is usually a bad idea
import vs from … import
| Approach | How you call a function |
|---|---|
import arithops |
arithops.sumop(10, 20) |
from arithops import sumop |
sumop(10, 20) |
from … import copies specific names into your program's namespace. You use them directly — no module prefix.
Think of it like a toolbox
import |
from … import |
|
|---|---|---|
| Analogy | Bring the whole toolbox into your room | Take only the tools you need |
| Code | import arithops |
from arithops import sumop |
| Usage | arithops.sumop(10, 20) |
sumop(10, 20) |
Interview memory rule:
import= module name required.from … import= direct function/variable.
3 syntaxes you must know
Syntax 1 — import specific names
Use this when you need only a few variables or functions.
from bankinfo import BNAME, ADDR, simple_interest
print(BNAME)
simple_interest(100000, 2, 10)from arithops import sumop, subop, mulop
sumop(10, 20)
subop(5, 6)
mulop(10, 2)No module prefix needed. Pick exactly the names you want — comma-separate multiple names.
Namespace visualization
The imported names are copied into your program's namespace, so you call them directly.
When Python runs from bankinfo import BNAME:
- Loads
bankinfo.py - Finds the name
BNAME - Creates a copy of that name in
main.py's namespace - You use
BNAMEdirectly — nobankinfo.prefix
Syntax 2 — import with aliases on names
Sometimes a function name is long or clashes with another name. Alias individual features.
from bankinfo import BNAME as bn, ADDR as ad, simple_interest as si
from arithops import sumop as addfn, subop as subfn
print(bn, ad)
si(100000, 2, 10)
addfn(2, 3)
subfn(10, 20)Here you alias individual variables and functions — something plain import cannot do.
Syntax 3 — import everything (*)
from mathconstants import *
print(PI)
print(E)import * pulls every public name from the module. It works, but:
- Imports names you may never use
- Makes it unclear where a name came from
- Can overwrite your own variable names accidentally
Recommendation: prefer Syntax 1 or 2 in real projects. Use * only in quick experiments or REPL sessions.
When should I use each?
| Situation | Best choice |
|---|---|
| Large utility module | import module |
| Need 2–3 functions | from module import func |
| Long function names | from module import func as alias |
| Professional production code | Avoid import * |
Side-by-side comparison
| Feature | import mod |
from mod import x |
|---|---|---|
| Module prefix required | Yes | No |
| Alias module name | Yes (as) |
No (module name not imported) |
| Alias variable/function | No | Yes (x as alias) |
| Import all names | No | Yes (import *) |
Folder structure (real project)
Imagine these files are in the same folder:
Inside main.py:
from bankinfo import BNAME
from arithops import sumop
print(BNAME)
sumop(10, 20)All .py files must live in the same directory for this simple layout to work.
Common mistakes
| Wrong | Correct |
|---|---|
import arithops then sumop(10, 20) |
import arithops then arithops.sumop(10, 20) |
from mathconstants import * then PI = 100 |
from mathconstants import PI then print(PI) |
| Mixing styles without thinking | Pick one style per module and stay consistent |
Golden rule: After from … import name, use name directly — not module.name.
Interview cheat sheet
| Syntax | Use |
|---|---|
import module |
Keep the module namespace |
from module import x |
Direct access to selected names |
from module import x as y |
Rename imported names |
from module import * |
Generally avoid in real projects |
Remember: import loads the module — access with module.feature. from … import copies names — access directly.
Key takeaways
- Toolbox analogy — whole box vs pick specific tools
- Syntax 1 —
from bankinfo import BNAME, simple_interest - Syntax 2 —
from arithops import sumop as addfn - Syntax 3 —
from mathconstants import *(avoid in production) importaliases modules;from-importaliases individual features- Next: what happens when you edit a module after it is already imported —
importlib.reload()
Next lesson
The next topic is reloading modules — useful when you change a .py file while a program is still running:
import importlib
import arithops
importlib.reload(arithops)That lets you pick up changes without restarting the whole program.
Loading playground…
Check your understanding
from import quiz
After `from arithops import sumop`, you call addition with:
1 / 10