6: Modules and Packages
6.1 Modules: Organizing Your Code
A module in Python is simply a file with the .py
extension that contains code. By splitting your program into modules, you can organize functions, classes, and variables into logical files. This prevents having one giant file and makes your code more readable and manageable.
Creating and Using a Module
Imagine you have a file named math_operations.py
with the following functions:
# math_operations.py
def add(a, b):
return a + b
def subtract(a, b):
return a - b
To use these functions in another file, for example, main.py
, you need to import the module.
# main.py
# Import the entire module
import math_operations
# Use the module’s functions
sum_result = math_operations.add(10, 5)
print(f"The sum is: {sum_result}")
sub_result = math_operations.subtract(10, 5)
print(f"The subtraction is: {sub_result}")
Ways to Import
There are several ways to import modules, each with its advantages:
-
Full import:
import module_name
- Advantage: Avoids name conflicts.
- Disadvantage: Code can be longer.
-
Specific import:
from module_name import function_or_class
- Advantage: Allows you to use functions directly without the module prefix.
- Disadvantage: Might cause conflicts if you import functions with the same name from different modules.
# main.py
from math_operations import add
result = add(20, 10)
print(f"The result is: {result}")
-
Import with alias:
import module_name as alias
- Advantage: Useful to shorten long module names.
import math_operations as m_ops
result = m_ops.add(7, 3)
print(f"The sum is: {result}")
6.2 Packages: Organizing Modules
A package is a collection of modules organized in a directory. Packages help you structure your project in a hierarchical and logical way, especially when you have many modules. A directory becomes a package if it contains a special file named __init__.py
(although in recent Python versions it is no longer strictly required, it is still good practice to include it).
Package Structure
Imagine the following file structure:
my_project/
├── main.py
└── mathematics/
├── __init__.py
├── basics.py
└── advanced.py
mathematics
is the package.basics.py
andadvanced.py
are the modules inside the package.
Importing from a Package
To use the functions from the basics.py
module in main.py
, you must specify the full path from the root of your project.
# main.py
from mathematics import basics
print(basics.add(2, 2))
You can also import the function directly:
# main.py
from mathematics.basics import add
print(add(10, 10))
6.3 Libraries: Third-Party Modules
In addition to the modules you create, Python has a vast community that has developed libraries (collections of packages and modules) for almost any task. Some popular libraries are NumPy for scientific computing, Pandas for data analysis, and requests for making web requests. To install them, you use the pip tool, which comes bundled with Python.
Using pip
To install a library, open your terminal and run:
pip install library_name
Once installed, you can import it into your code like any other module. For example, to use the requests
library:
# In the terminal
pip install requests
# In your Python code
import requests
response = requests.get("https://www.google.com")
print(response.status_code)
By mastering the use of modules and packages, you’ll be able to scale your projects, keep your code clean, and take advantage of Python’s vast ecosystem of libraries. Congratulations, you’ve completed the fundamentals of Python! Now it’s time to put everything you’ve learned into practice in your own projects.