⭐ Importing Modules in Python
After creating a module or using a built-in module, we must import it to use the functions, variables, and classes defined inside it.
Python provides multiple ways to import modules depending on the requirement.
⭐ 1. Basic Import Statement
Syntax
import module_name
Example
import math
print(math.sqrt(16))
✔ Fully qualifies names using module_name.function()
✔ Recommended for readability
⭐ 2. Importing a Module with an Alias
Alias = short name for convenience.
Syntax
import module_name as alias
Example
import numpy as np
print(np.array([1,2,3]))
✔ Makes code shorter
✔ Useful for lengthy module names (e.g., numpy → np)
⭐ 3. Importing Specific Members from a Module
To import only selected functions or variables.
Syntax
from module_name import function1, function2
Example
from math import sqrt, pi
print(sqrt(25))
print(pi)
✔ No need to prefix with module name
❌ Can cause naming conflicts if function names are common
⭐ 4. Importing All Members of a Module (Wildcard Import)
Syntax
from module_name import *
Example
from math import *
print(sin(1))
print(cos(1))
⚠ Not recommended because:
- It imports everything, polluting the namespace
- Overwrites existing function names unknowingly
Used only when:
✔ writing small programs
✔ working interactively
⭐ 5. Importing User-Defined Modules
If you create a file mymodule.py:
# mymodule.py
def greet(name):
return f"Hello {name}"
Import it:
import mymodule
print(mymodule.greet("Balvinder"))
✔ Python searches for modules in the current directory first
⭐ 6. Importing Modules from a Different Folder
If the module is in another folder, add the path:
import sys
sys.path.append("C:/myfolder")
import mymodule
⭐ 7. Reloading a Module (When Code Changes)
When you change the module file after importing, Python does not refresh automatically.
Use:
import importlib
importlib.reload(mymodule)
⭐ 8. Importing Inside a Function
You can also import a module within a function:
def calculate():
import math
return math.sqrt(16)
Used when importing module only if needed.
⭐ How Python Finds Modules? (Module Search Path)
Python searches modules in this order:
1️⃣ Current working directory
2️⃣ PYTHONPATH environment variable
3️⃣ Installed site-packages (libraries)
4️⃣ Built-in modules
Check search locations:
import sys
print(sys.path)
⭐ Why Multiple Import Methods?
| Import Style | Use Case |
|---|---|
import module | Best for readability; full access |
import module as alias | Shortening long module names |
from module import item | When using specific functions often |
from module import * | Quick experimentation; NOT for production |
⭐ Exam-Ready Short Answer
Importing a module in Python means loading its functions, classes, and variables into the current program. Python provides different import styles such as: import module, import module as alias, from module import item, and from module import *. Importing improves code reusability, modularity, and allows access to built-in and user-defined functionality.
