Python Modules

A Python Modules are a file consisting of Python codes. Moreover, Module allows you to consistently organize your Python code. Gathering related code into a module makes the code simpler to get and utilize. A module can define functions, classes, and variables. Python modules  could be, Python question with subjectively named properties that simply can tie and reference.

Example:
def sum1(a, b):
    return (a+b)
 
def sum2(x, y):
    return (x-y)

The import statement: 

Any Python source file can be used as a module by executing an 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.

Syntax:

For better understanding given below is the example,

#Import module 
import support

#call defined function that module follows
support.print_func("Bob")

The from import Statement:

Python’s from statement import specific attributes from a module into the current namespace.

Syntax,

The from import* Statement:

It is possible to import all names from a module into the current namespace by using import* statement.

Syntax,

The PYTHONPATH Variable:

The PYTHONPATH is an environment variable, consisting of a list of directories. The syntax of PYTHONPATH is the same as that of the shell variable PATH.

Syntax for typical PYTHONPATH from a Windows system,

Syntax for typical PYTHONPATH from a UNIX system,

Namespaces and Scoping:

As we discussed earlier, the names/identifiers assigned to objects are known as Variables. A Namespace is a dictionary of those variable names/keys and their corresponding objects/values. Those objects can be a variable or a method. Python itself maintains a namespace in the form of a Python Dictionary. To better understand the meaning of namespace you can take it as a surname.

A Python statement can easily access variables in a local namespace and in the global namespace well. In this matter, where a local and a global variable have the same name, the local variable shadows the global variable. Each function has assigned its own local namespace. Ordinary functions and Class methods follow the same scoping rule.

Example:
#variable1 is in the global namespace 

variable1 = 10
def some_func():
 
#variable2 is in the local namespace 
 
 variable2= 5
    def some_inner_func():
 
       

In some cases, one can be interested in updating or processing global variables only, one should mark it explicitly as global and the update or process.  

#program processing
#global variable
 
Value = 8
def Addvalue():
    global count
    Value = Value + 1
    print(Value)
Addvalue()

The dir( ) Function:

The dir() built-in function returns a sorted list of strings containing the names defined by a module. The list contains the names of all the modules, variables, and functions that are defined in a module.

# Import built-in module math

import math

content = dir(math)
print content

The globals() and locals() Functions:

Some Functions can be used to return the names in the global and local namespaces depending on the location from where they are called, those functions known as globals() and locals(). The return type of both these functions is a dictionary. Therefore, Using the keys() function, names can be extracted.

If locals() Function is called from within a function, it will return all the names that can be accessed locally from that function.

If the globals() function is called from within a function, it will return all the names that can be accessed globally from that function.

The reload() Function:

When the module is imported into a script, the code within the top-level parcel of a module is executed as it were once.

Hence, in case you need to re-execute the top-level code in a module, you’ll utilize the reload() work. The reload() work imports an already imported module once more.

Syntax of the reload() function:

Packages in Python:

package maybe a hierarchical file directory structure that characterizes a single Python application environment that comprises modules and sub-packages and sub-sub packages, and so on.