In Python, modules are a way to organize and reuse code. Importing modules allows you to access a module’s functions, classes, and variables in your program. In this tutorial, we’ll learn how to import built-in modules, import specific functions or classes, and even create and use custom modules.
1. What is a Python Module?
A Python module is a file with a .py extension that contains Python code—such as functions, classes, and variables. Modules allow you to logically organize your Python programs and reuse code across different programs.
2. Importing a Python Module
To use the functions and variables in a module, you need to import it. The most basic way to import a module is by using the import statement:
Example: Importing a Built-in Module
import math result = math.sqrt(16) print(result)
Here, we import the math module and use its sqrt() function to calculate the square root of 16.
3. Importing Specific Functions or Classes from a Module
If you only need specific functions or classes from a module, you can import them directly using the from keyword. This avoids the need to reference the module name every time you call a function or class.
Example: Importing Specific Functions
from math import sqrt result = sqrt(16) print(result)
Here, we import the sqrt() function directly from the math module, so we don’t need to reference math.sqrt() each time.
4. Renaming a Module on Import
You can rename a module when importing it using the as keyword. This is especially useful when dealing with long module names or to avoid name conflicts.
Example: Renaming a Module
import math as m result = m.sqrt(16) print(result)
In this example, we import the math module and rename it as m. Now we can reference the module with the shorter name m.
5. Importing All Functions from a Module
Another way to import a module is by importing everything from the module. This allows you to directly access all functions, classes, and variables without needing to reference the module name.
Example: Importing All Functions
from math import * result = sqrt(16) print(result)
Here, we import everything from the math module, so we can use the sqrt() function directly without referencing math.sqrt().
6. Importing from a Custom Module
In addition to built-in modules, you can also create your own custom modules. A custom module is just a Python file containing functions or classes that you want to reuse in other scripts.
Example: Creating a Custom Module
# my_module.py
def add(a, b):
return a + b
Now, you can import and use the add() function from your custom module in another Python file.
Example: Using a Custom Module
import my_module result = my_module.add(5, 3) print(result)
In this example, we import the my_module module and call the add() function defined inside it.
7. Importing from a Module in a Different Directory
If your module is located in a different directory, you can specify its path using the sys.path variable or by modifying the PYTHONPATH environment variable.
Example: Importing from a Different Directory
import sys
sys.path.append('/path/to/your/module/directory')
import my_module
In this example, we add the directory containing our custom module to the system path, allowing us to import the module from that location.
Conclusion
Importing modules is a powerful way to organize and reuse code in Python. You can import built-in modules, specific functions or classes, and even your custom modules to make your programs more modular and efficient.