Python Modules: Creation, Importing, and Advanced Usage
Python Modules
Discover how to build, import, and manage Python modules. Learn best practices, advanced import techniques, and module search paths.
Video: Python Modules
Watch an in‑depth tutorial on module fundamentals.
What Are Python Modules?
A module is simply a file containing Python statements and definitions. For example, example.py is a module named example.
Modules help you break large programs into manageable, reusable pieces. Define common functions once and import them wherever needed.
Let’s create a small module. Save the following as example.py:
# example.py – a simple addition module
def add(a, b):
"""Return the sum of two numbers."""
return a + b
How to Import Modules in Python
Use the import keyword to bring a module into the current namespace. For example:
>>> import example
Access its contents via the dot operator:
>>> example.add(4, 5.5)
9.5
Python ships with a rich standard library. You can browse all modules at Python Standard Library.
Python Import Statement
# Import the standard math module
import math
print("The value of pi is", math.pi)
The value of pi is 3.141592653589793
Import with Renaming
# Import math as a shorter alias
import math as m
print("The value of pi is", m.pi)
Renaming saves typing, but remember the original name (math) is no longer available.
Python from…import Statement
# Import only the constant pi
from math import pi
print("The value of pi is", pi)
Multiple names can be imported in one line:
from math import pi, e
print(pi, e)
Import All Names
from math import *
print("The value of pi is", pi)
Using * is discouraged because it pollutes the namespace and can obscure where a name came from.
Python Module Search Path
When Python imports a module, it checks:
- The current directory.
- Directories listed in the
PYTHONPATHenvironment variable. - Python’s installation‑specific default directories.
Inspect the path list with sys.path:
import sys
print(sys.path)
# ['', 'C:\Python33\Lib\idlelib', 'C:\Windows\system32\python33.zip', ...]
Reloading a Module
Modules are loaded only once per interpreter session. To re‑evaluate a changed module, use importlib.reload() (the imp module is deprecated in Python 3). Example:
# my_module.py
print("This code got executed")
>>> import my_module
This code got executed
>>> importlib.reload(my_module)
This code got executed
The dir() Built‑in Function
Use dir() to list all attributes of a module:
import example
print(dir(example))
# ['__builtins__', '__cached__', '__doc__', '__file__', '__name__', 'add']
Special attributes such as __name__ reveal module metadata:
print(example.__name__)
# 'example'
Calling dir() without arguments shows all names in the current namespace.
Python
- Mastering Python I/O and Module Imports: A Practical Guide
- Mastering Python Operators: A Comprehensive Guide
- Python Modules: Creation, Importing, and Advanced Usage
- Python List Operations: Creation, Access, Modification, and Advanced Techniques
- Mastering Python Tuples: Creation, Access, and Advanced Operations
- Mastering Python Dictionaries: Creation, Manipulation, and Advanced Techniques
- Mastering Python’s time Module: Functions, Structs, and Practical Examples
- Python Module Importing – A Practical Guide with Examples
- Python Modules: Organizing Your Code for Clarity and Reusability
- Master Bluetooth Interfacing: A Practical Guide to Connecting Bluetooth Modules with Arduino