PythonModules
What is a Module?
Consider a module to be the same as a code library.
A file containing a set of functions you want to include in your application.
Create a Module
To create a module just save the code you want in a file with the file extension.py
:
Example
Save this code in a file namedmymodule.py
print("Hello, " + name)
Use a Module
Now we can use the module we just created, by using theimport
statement:
Example
Import the module named mymodule, and call the greeting function:
mymodule.greeting("Jonathan")
Note: When using a function from a module, use the syntax:module_name.function_name.
Variables in Module
The module can contain functions, as already described, but also variables of all types (arrays, dictionaries, objects etc):
Example
Save this code in the filemymodule.py
"name": "John",
"age": 36,
"country": "Norway"
}
Example
Import the module named mymodule, and access the person1 dictionary:
a = mymodule.person1["age"]
print(a)
Naming a Module
You can name the module file whatever you like, but it must have the file extension.py
Re-naming a Module
You can create an alias when you import a module, by using theas
keyword:
Example
Create an alias formymodule
calledmx
:
a = mx.person1["age"]
print(a)
Built-in Modules
There are several built-in modules in Python, which you can import whenever you like.
Example
Import and use theplatform
module:
x = platform.system()
print(x)
Using the dir() Function
There is a built-in function to list all the function names (or variable names) in a module. Thedir()
function:
Example
List all the defined names belonging to the platform module:
x = dir(platform)
print(x)
Note: The dir() function can be used onall modules, also the ones you create yourself.
Import From Module
You can choose to import only parts from a module, by using thefrom
keyword.
Example
The module namedmymodule
has one function and one dictionary:
print("Hello, " + name)
person1 = {
"name": "John",
"age": 36,
"country": "Norway"
}
Example
Import only the person1 dictionary from the module:
print (person1["age"])
Note: When importing using thefrom
keyword, do not use the module name when referring to elements in the module. Example:person1["age"]
,notmymodule.person1["age"]