-
1. Introduction to Python
-
2. Python Basics
-
3. Working with Data Structures
-
4. Functions and Modules
-
5. Object-Oriented Programming (OOP)
-
6. File Handling
-
7. Error and Exception Handling
-
8. Python for Data Analysis
-
9. Advanced Topics in Python
-
10. Working with APIs
-
11. Python for Automation
-
12. Capstone Projects
- 13. Final Assessment and Quizzes
4.3.1 Creating custom modules
A module in Python is simply a file containing Python code (functions, classes, variables, etc.). You can create your own modules by writing code in a .py file, which can then be imported and used in other Python scripts.
Creating custom modules allows you to organize your code into reusable components. Here's how you can create and use custom modules in Python:
1. Creating a Custom Module
To create a custom module, follow these steps:
- Write your Python code in a file with a .py extension.
- Save the file with an appropriate name. For example, you might save it as my_module.py.
Example my_module.py:
# my_module.py def greet(name): return f"Hello, {name}!" def add_numbers(a, b): return a + b
In this example, we have two functions in the module: greet() and add_numbers().
2. Using Your Custom Module
Once you have created a custom module, you can use it in another Python script by importing it.
- Import the module using the import statement.
- Call the functions or use variables defined in the module.
Example main.py:
# main.py import my_module # Importing the custom module # Using the greet function from the module print(my_module.greet("Alice")) # Output: Hello, Alice! # Using the add_numbers function from the module result = my_module.add_numbers(5, 3) print(result) # Output: 8
3. Importing Specific Functions from a Custom Module
Instead of importing the entire module, you can import only specific functions, classes, or variables from your module.
Example main.py:
# main.py from my_module import greet # Importing only the greet function # Using the greet function directly print(greet("Bob")) # Output: Hello, Bob!
4. Using Aliases for Modules
You can give a custom name (alias) to the imported module using the as keyword. This is useful if the module's name is long or conflicts with another module.
Example main.py:
# main.py import my_module as mm # Importing with an alias # Using functions with the alias print(mm.greet("Charlie")) # Output: Hello, Charlie!
5. Folder Structure and Packages
If your module is part of a larger project, you might want to organize it into a package (a collection of modules). A package is a directory that contains multiple .py files and an __init__.py file, which indicates that the directory is a Python package.
For example, you can create a package structure like this:
my_project/ my_package/ __init__.py my_module.py main.py
- __init__.py: This file can be empty but must be present for the directory to be considered a package.
- my_module.py: Your custom module.
- main.py: The script where you import and use the module.
In main.py, you can now import from the package:
# main.py from my_package import my_module # Importing from the package print(my_module.greet("Dave")) # Output: Hello, Dave!
6. Reloading Custom Modules
If you make changes to a custom module while running a Python script and want to see the updates without restarting the interpreter, you can use the reload() function from the importlib module.
Example:
from importlib import reload import my_module # Reload the module to apply changes reload(my_module)
7. Testing Your Custom Module
It’s often a good practice to add testing code to your custom module. This can help ensure that your module behaves as expected. You can use the __name__ variable to test if the module is being run directly or imported.
Example my_module.py:
# my_module.py def greet(name): return f"Hello, {name}!" def add_numbers(a, b): return a + b # Testing the module if __name__ == "__main__": print(greet("Test")) print(add_numbers(2, 3))
When you run my_module.py directly, the testing code will execute. However, if it’s imported into another script, the testing code will be skipped.
8. Summary
- A module is a file containing Python code that can define functions, classes, and variables.
- You create a custom module by saving Python code in a .py file.
- Use the import statement to access functions and classes in your custom module.
- You can also import specific functions or assign aliases to modules.
- Organize your modules into packages for larger projects.
- Test your modules by including code under the if __name__ == "__main__": condition to ensure it’s only executed when the module is run directly.
Creating custom modules allows you to organize, reuse, and share your Python code more effectively.
Commenting is not enabled on this course.