Skip to main content
MahiDev — Learn. Build. Become AI-ready.

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.

Intermediate35 min
from import in Python hero — Module 14 Lesson 4 toolbox vs tools

Memory trick: import brings the whole toolbox. from … import takes only the tools you need.

Use the playground and quiz at the bottom to practice all three syntaxes.

What you will learn

  • How from … import differs from plain import
  • 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

import arithops module prefix vs from arithops import sumop direct call
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 whole toolbox vs from import pick specific tools analogy
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

Three from-import syntaxes named imports alias and star import

Syntax 1 — import specific names

Use this when you need only a few variables or functions.

Syntax 1 from bankinfo import BNAME ADDR simple_interest no prefix
python
from bankinfo import BNAME, ADDR, simple_interest

print(BNAME)
simple_interest(100000, 2, 10)
python
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

from bankinfo import names copied into main.py namespace diagram

The imported names are copied into your program's namespace, so you call them directly.

When Python runs from bankinfo import BNAME:

  1. Loads bankinfo.py
  2. Finds the name BNAME
  3. Creates a copy of that name in main.py's namespace
  4. You use BNAME directly — no bankinfo. prefix

Syntax 2 — import with aliases on names

Sometimes a function name is long or clashes with another name. Alias individual features.

Syntax 2 from arithops import sumop as addfn alias on names
python
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 (*)

Syntax 3 from mathconstants import star avoid in production
python
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:

project folder bankinfo arithops mathconstants main.py structure

Inside main.py:

python
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

from import interview cheat sheet import vs from import star
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)
  • import aliases modules; from-import aliases 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:

python
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