Understanding Python Modules
A module in Python is essentially a code library—a file that encapsulates a collection of functions, variables, or classes that you can include in your application. Modules are a powerful way to organize and reuse code across different projects.
Creating Your Own Module
To create a module, simply write the functions or variables you want to include in a file with the .py
extension.
Example: Save the following code in a file named mymodule.py
.
def greeting(book_name):
print("Hello, " + book_name)
In this example, the greeting
function is defined to greet a specific book by its name.
Using a Custom Module
Once you’ve created your module, you can import it into any Python script using the import
statement.
Example: Import the module mymodule
and call its greeting
function.
import mymodule
mymodule.greeting("The Great Gatsby")
# Expected output: Hello, The Great Gatsby
When you call a function from a module, the syntax is straightforward: module_name.function_name
. The result is visible in the comment above.
Including Variables in a Module
In addition to functions, a module can also contain variables of any type, such as arrays, dictionaries, or objects.
Example: Save this code in mymodule.py
.
book1 = {
"title": "1984",
"pages": 328,
"author": "George Orwell"
}
Now, you can import the module and access the variables it contains.
import mymodule
a = mymodule.book1["pages"]
print(a)
# Expected output: 328
Naming and Renaming Modules
You can name your module file whatever you like, as long as it has the .py
extension. Additionally, when importing a module, you can create an alias using the as
keyword.
Example: Create an alias for mymodule
and access its variables.
import mymodule as mx
a = mx.book1["pages"]
print(a)
# Expected output: 328
Exploring Built-in Modules
Python comes with several built-in modules that you can import and use without needing to create them yourself.
Example: Import the platform
module and use it to determine the system type.
import platform
x = platform.system()
print(x)
# Expected output could be: Linux, Windows, or Darwin (for macOS)
Using the dir()
Function
The dir()
function is a built-in Python function that lists all the function names (or variable names) defined in a module.
Example: List all the attributes of the platform
module.
import platform
x = dir(platform)
print(x)
# Expected output: A list of all attributes in the platform module, such as ['architecture', 'machine', 'node', 'platform', 'processor', 'python_version', 'system', etc.]
The dir()
function can be used on any module, including those you create yourself.
Importing Specific Elements from a Module
Sometimes, you might want to import only certain parts of a module. This can be done using the from
keyword.
Example: The mymodule
module contains a function and a dictionary:
def greeting(book_name):
print("Hello, " + book_name)
book1 = {
"title": "1984",
"pages": 328,
"author": "George Orwell"
}
You can choose to import only the dictionary book1
from the module.
from mymodule import book1
print(book1["pages"])
# Expected output: 328
Note that when you use the from
keyword, you should refer to the imported elements directly, without using the module name.