Python Mastery – From Basics to Brilliance / Modules & Packages

import vs from...import

📦 import vs from ... import

Both are used to import modules or specific parts of modules into a Python program.


1️⃣ import

Imports the entire module.

import math

print(math.sqrt(25))
print(math.pi)

Here, we use the module name math to access its functions and values.


2️⃣ from ... import

Imports specific functions or objects from a module.

from math import sqrt

print(sqrt(25))

Here, only sqrt is imported, so we don't need to write math.sqrt().

You can import multiple items:

from math import sqrt, pi

print(sqrt(25))
print(pi)

⚖️ Difference

import 📦

from ... import 🎯

Imports the module

Imports specific items

import math

from math import sqrt

Use math.sqrt()

Use sqrt()

Keeps the module namespace clear

More convenient for specific items

import → Bring the module

from ... import → Bring specific things from the module