In Python, a module is simply a .py file that contains functions, classes, or variables. You can import these modules into other programs to reuse code, keep your projects organized, and avoid rewriting the same logic again and again.
.py files that can be imported in other files.math, random, and datetime that are ready to use.? Basic import
Import the whole module and access its members using module_name.item:
# Import the entire math module
import math
# Call the sqrt() function from math
result = math.sqrt(16)
✂️ Import specific names
Import only what you need using from module import name. You can import multiple items separated by commas:
# Import only sqrt and pi from math
from math import sqrt, pi
# Use the imported names directly
value = sqrt(25) + pi
? Aliasing
Use as to create a shorter name (alias) for a module or function. This is common for long module names.
# Give the math module a shorter alias
import math as m
# Import factorial with a shorter alias
from math import factorial as fact
# Use the aliased module and function
a = m.sqrt(49)
b = fact(5)
? Custom modules
Create your own file like my_module.py and import it inside main.py. Both files should be in the same folder (or in the Python path).
# my_module.py
def greet(name):
return f"Hello, {name}!"
# main.py
import my_module
message = my_module.greet("Alice")
print(message)
# Import the math module from the standard library
import math
# Use functions and constants from the math module
print(math.sqrt(16)) # 4.0
print(math.pi) # 3.141592653589793
# Import only the names you need from math
from math import sqrt, pi
# Call sqrt() and use pi directly
print(sqrt(25)) # 5.0
print(pi) # 3.141592653589793
# Alias the math module and the sqrt() function
import math as m
from math import sqrt as square_root
# Use the aliases instead of full names
print(m.factorial(5)) # 120
print(square_root(36)) # 6.0
# my_module.py
def greet(name):
return f"Hello, {name}!"
# main.py
import my_module
# Call the greet() function from the custom module
print(my_module.greet("Alice")) # Hello, Alice!
math.sqrt(16) → 4.0 (square root of 16)math.pi → 3.141592653589793 (value of π)sqrt(25) (imported directly) → 5.0m.factorial(5) → 120square_root(36) → 6.0my_module.greet("Alice") → Hello, Alice!Notice how importing specific items (like sqrt) lets you use them directly, while importing the whole module (like math) keeps the namespace clear using the module_name.item pattern.
math, random, datetime, etc.) instead of writing everything from scratch.from module import * in real projects — it can overwrite names and reduce readability.ModuleNotFoundError, check that:
math for mathematical operations like roots, trigonometry, and constants.random for games, simulations, and test data.datetime to work with dates and times.helpers.py) to store frequently used functions.random module and generate a random integer between 1 and 10.add(a, b) and multiply(a, b)) and import them in another file.import random as r.from ... import ....