Module intro

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.
A Python module is a file containing Python definitions and statements.
A module can define functions, classes, and variables.
A module can also include runnable code.
Grouping related code into a module makes the code easier to understand and use.
It also makes the code logically organized.

Creating a Module

To create a module just save the code you want in a file with the file extension .py: Example: Save code in a file named mymodule.py

def greeting(web):
    print("Rgukt"+web)
Using a Module

We can import the functions, and classes defined in a module to another module using the import statement in some other Python source file.
When the interpreter encounters an import statement, it imports the module if the module is present in the search path.
A search path is a list of directories that the interpreter searches for importing a module.
For example, to import the module calc.py, we need to put the following command at the top of the script.

import calc
print(calc.add(10, 2))

#Output is 12

Variables in Modules

The module can contain functions, as already described, but also variables of all types (arrays, dictionaries, objects etc):
save this file as mymodule.py

person = {
  "name": "John",
  : 36,
  "country": "Norway"
}

importing mymodule file

import mymodule
a = web.person["age"]
print(a)
Naming & Renaming a module

Naming a Module You can name the module file whatever you like, but it must have the file extension .py Renaming a Module You can create an alias when you import a module, by using the as keyword:

import mymodule as mx

a = mx.person1["age"]
print(a)
Built-in Modules

A module is a file containing definition of functions, classes, variables, constants or any other Python object.
Standard distribution of Python contains a large number of modules, generally known as built-in modules.
There are several built-in modules in Python, which you can import whenever you like.
Import and use the platform module:

import platform

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. The dir() function
The dir() function returns all properties and methods of the specified object, without the values.
This function will return all the properties and methods, even built-in properties which are default for all object.
Example
List all the defined names belonging to the platform module:

import platform

x = dir(platform)
print(x)

Note: The dir() function can be used on all modules, also the ones you create yourself.

Import From Module

Python’s from statement lets you import specific attributes from a module without importing the module as a whole.
Import Python modules can get access to code from another module by importing the file/function using import.
The import statement is the most common way of invoking the import machinery, but it is not the only way.
Example
The module named mymodule has one function and one dictionary:

def greeting(name):
  print("Hello, "+ name)

web_developper = {
  "name": "rguktian",
  "age": 18,
  "country": "india"
}

Import only the person1 dictionary from the module:

from mymodule import web_developper

print (web_developper["name"])

Note: When importing using the from keyword, do not use the module name when referring to elements in the module.
Example: web_developper["name"], not mymodule.web developper["name"]

Locating Python Modules

Whenever a module is imported in Python the interpreter looks for several locations.
First, it will check for the built-in module, if not found then it looks for a list of directories defined in the sys.path.
Python interpreter searches for the module in the following manner –

import random
import os

fpath = random.__file__
path = os.path.dirname(fpath)
print(path)