A module is essentially a Python file with a .py extension that contains definitions of functions, classes, and variables. When you want to use those functions or classes in your program, you import the module.
There are two primary ways to import modules in Python:
You can import specific functions from a module, which makes the code more concise and readable. Here's how to do it:
Suppose you have a module file math_operations.py with the following content:
# math_operations.py
def add(x, y):
return x + y
def subtract(x, y):
return x - y
def multiply(x, y):
return x * y
def divide(x, y):
if y == 0:
return "Cannot divide by zero"
return x / yYou can then import and use specific functions from this module:
# main.py
from math_operations import add, subtract
result1 = add(5, 3) # Calls the 'add' function from math_operations.py
result2 = subtract(5, 3) # Calls the 'subtract' function
print(f"Addition result: {result1}")
print(f"Subtraction result: {result2}")Alternatively, you can import all functions from a module using the * operator. However, it’s generally better to import only what you need to avoid unnecessary imports.
# main.py
from math_operations import *
result1 = add(10, 5) # Directly use 'add' and 'subtract' without qualifying module name
result2 = subtract(10, 5)
print(f"Addition result: {result1}")
print(f"Subtraction result: {result2}")You can also import the entire module and access its functions by qualifying them with the module name:
# main.py
import math_operations
result1 = math_operations.add(7, 2) # Use module name to access functions
result2 = math_operations.subtract(7, 2)
print(f"Addition result: {result1}")
print(f"Subtraction result: {result2}")Organizing your code into smaller, logical modules makes your code cleaner and more maintainable. For example, if you are working on a financial system, you might have the following modules:
tax_calculations.py: Handles tax-related functions.salary_calculations.py: Handles salary-related functions.employee_data.py: Handles employee-related data.
salary_calculations.py
# salary_calculations.py
def calculate_annual_salary(monthly_salary):
return monthly_salary * 12
def calculate_bonus(annual_salary, bonus_percentage):
return annual_salary * (bonus_percentage / 100)tax_calculations.py
# tax_calculations.py
def calculate_tax(annual_salary, tax_rate):
return annual_salary * (tax_rate / 100)
def calculate_net_salary(annual_salary, tax_amount):
return annual_salary - tax_amountemployee_data.py
# employee_data.py
def get_employee_name(employee_id):
# In a real case, this would query a database or an API.
return f"Employee {employee_id}"
def get_employee_salary(employee_id):
# Just a mock salary for this example
return 5000main.py
# main.py
from salary_calculations import calculate_annual_salary, calculate_bonus
from tax_calculations import calculate_tax, calculate_net_salary
from employee_data import get_employee_name, get_employee_salary
def main():
employee_id = 101
name = get_employee_name(employee_id)
monthly_salary = get_employee_salary(employee_id)
annual_salary = calculate_annual_salary(monthly_salary)
bonus = calculate_bonus(annual_salary, 10) # 10% bonus
tax = calculate_tax(annual_salary, 15) # 15% tax rate
net_salary = calculate_net_salary(annual_salary, tax)
print(f"{name}'s Annual Salary: {annual_salary}")
print(f"Bonus: {bonus}")
print(f"Tax: {tax}")
print(f"Net Salary: {net_salary}")
if __name__ == "__main__":
main()If you want to create a Python package, which is a collection of related modules, you can structure your project directory like this:
my_project/
├── employee_data.py
├── salary_calculations.py
├── tax_calculations.py
├── __init__.py
└── main.py
- The
__init__.pyfile makes the directory a Python package. It can be left empty or contain initialization code for the package.
You can then import the modules in main.py:
from my_project.salary_calculations import calculate_annual_salary
from my_project.tax_calculations import calculate_taxTo make sure your code is well-organized and easy to reuse, follow these best practices:
- Naming Conventions: Use descriptive names for modules and functions, which indicate their purpose.
- Directory Structure: Keep related modules in separate directories if needed. For example:
finance/: Contains all financial-related modules.employees/: Contains modules for employee data and payroll calculations.
- Use Version Control: Save your code in a version control system like Git to track changes and collaborate effectively.
In Python, modules are a great way to organize your code into smaller, reusable pieces. By importing specific functions or entire modules, you can make your code cleaner and more maintainable.
Here are the key points to remember:
- Use
import module_nameto import the entire module. - Use
from module_name import function_nameto import specific functions. - For large projects, organize your code into separate modules and use packages.
- Use version control (e.g., Git) to manage and track your code.