← Back to Chapters

Python Modules & Imports

? Python Modules & Imports

⚡ Quick Overview

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.

? Key Concepts

  • Module: A Python file containing reusable code.
  • import: Used to bring an entire module into your program.
  • from ... import ...: Import specific functions, classes, or variables from a module.
  • Alias (as): Give a shorter or more convenient name to a module or function.
  • Custom modules: Your own .py files that can be imported in other files.
  • Standard library: Built-in modules like math, random, and datetime that are ready to use.

? Syntax & Theory

? 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)

? Code Examples

? Importing a standard library module
# 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
✂️ Importing specific items from a module
# 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
? Using aliases for modules and functions
# 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
? Creating and importing your own module
# 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!

? Output & Explanation

? What you’ll see when you run these examples

  • math.sqrt(16)4.0 (square root of 16)
  • math.pi3.141592653589793 (value of π)
  • sqrt(25) (imported directly) → 5.0
  • m.factorial(5)120
  • square_root(36)6.0
  • my_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.

? Helpful Tips

  • Use modules to organize code into logical files and promote reusability.
  • Leverage Python’s standard library (math, random, datetime, etc.) instead of writing everything from scratch.
  • Keep custom modules in the same directory as your main file (or add them to the Python path).
  • Avoid using from module import * in real projects — it can overwrite names and reduce readability.
  • If you get ModuleNotFoundError, check that:
    • The module file is saved.
    • The file name is correct (case-sensitive on some systems).
    • Your current working directory is set properly.

? Common Use Cases

  • Using math for mathematical operations like roots, trigonometry, and constants.
  • Using random for games, simulations, and test data.
  • Using datetime to work with dates and times.
  • Creating your own utility modules (e.g., helpers.py) to store frequently used functions.

? Practice Tasks

  • Import the random module and generate a random integer between 1 and 10.
  • Create a module with two functions (e.g., add(a, b) and multiply(a, b)) and import them in another file.
  • Use aliasing to shorten module or function names, such as import random as r.
  • Experiment with importing only specific items from a module using from ... import ....