Discover the fundamental differences between Python modules and packages in this detailed guide. Learn how modules—single Python files containing reusable code—compare to packages, which organize multiple modules into structured directories. Understand how to create, import, and use both modules and packages to streamline your Python projects. Explore practical examples, from creating basic modules to leveraging Python’s built-in sys.path
for locating and managing dependencies. Whether you’re a beginner or an experienced developer, this blog will help you master modular programming in Python for better organization, reusability, and scalability.
Aspect | Module | Package |
Definition | A single Python file with code (e.g., .py file). | organized in a directory. |
Structure | A single file. | multiple modules and as well as __init__.py file. |
Purpose | Organizes related code into a single file. | Organizes related modules into a directory structure. |
Importing | Imported using import module_name. | Imported using import package_name.module_name. |
Example | math.py, greetings.py | my_package/greetings.py, my_package/farewell.py |
__init__.py File | Not required. | directory as a package. |
—————————
A module is a single file that contains Python code, such as functions, variables, and classes.
It can also contain runnable code.
The primary purpose of a module is to break down large codebases into smaller, manageable, and reusable components.
————————————–
Mynew.py file
——————-
def greet(name)
return name;
def add(x,y)
return (x+y)
import a new module
——————————-
calculator.py
import mynew
print(mynew.greet(“iies”));
print(mynew.add(5,10))
From statements in Python:
————————————–
# importing sqrt() and factorial from the
# module math
from math import sqrt, factorial
# math.sqrt(16) and math.factorial()
# are required.
print(sqrt(16))
print(factorial(6))
from math import sqrt
printf(sqrt(16))
Import all Names
———————-
# importing sqrt() and factorial from the
# module math
from math import *
# math.sqrt(16) and math.factorial()
# are required.
print(sqrt(16))
print(factorial(6))
Example of Locating a Module:
import sysprint(sys.path)
Python first looks in its built-in standard library. For example, if you import the math module, Python will look in the standard library where math.py is located.
import mathprint(math.sqrt(16)) # Uses the math module from the standard library
If a module is in the same directory as the script, Python will find it there.
Example:
Suppose you have a file mymodule.py in the same directory as your script main.py:
# mymodule.pydef greet(name): return f”Hello, {name}!”
pip install requests
You can import it like this:
import requestsresponse = requests.get(‘https://www.example.com’)print(response.status_code)
Python will locate the requests module in the site-packages directory (e.g., /usr/lib/python3.9/site-packages/ on Linux) and allow you to use it.
5. Module Search Path (sys.path)
Python maintains a list of directories that it searches for modules, which is stored in sys.path. You can inspect this list to see where Python will search for modules.
Example:
import sysprint(sys.path)
Indian Institute of Embedded Systems – IIES