What is a Module? def greeting(name):      print("Hello, " + name)
A module can be considered to be same as a code library.
A file that contains a set of functions or methods that you want to include in your application.
Create a Module
If you want to create a module, then just save the code you want in your file with the file extension ".py":
And save this code in a file named "mymodule.py"
Use a Module import mymodule mymodule.greeting("Jonathan")
Now if we want ot use the module that we have have recently created then it can be done with the help of import statement.
For e.g. if we want to mmport the module named mymodule, and call the greeting function, then we write the following code:
Variables in Module person1 = { "name": "John", "age": 36, "country": "Norway" }
The module can contain methods/functions, as already described, but also variables of all types like arrays, dictionaries, objects etc.:
Note: Save this code in the file mymodule.py
Example: Import the module named mymodule, and access the person1 dictionary: import mymodule a = mymodule.person1["age"] print(a)
Naming a Module import mymodule as mx a = mx.person1["age"] print(a)
You can name the module file anything 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 the "as" keyword:
For e.g. create an alias for mymodule as mx:
Built-in Modules import platform x = platform.system() print(x)
There are several built-in modules in Python, which can be imported directly whenever you like to import them.
For e.g. import and use the platform module:
Using the dir() Function import platform x = dir(platform) print(x)
There dir() is a built-in function that will list all the function names or variable names in a module.
Example: List all the defined names belonging to the platform module:
Import From Module import platform def greeting(name):      print("Hello, " + name) person1 = { "name": "John", "age": 36, "country": "Norway" }
You can import only the parts of any module from that with the help of "from" keyword.
Example: The module named mymodule has one function and one dictionary:
For e.g. if we want to import only the person1 dictionary from the module: from mymodule import person1 print (person1["age"])