PYTHON MODULES

 

modules

  • Python modules contain different statements and  in addition functions defined inside.
  • In other words, the module can define functions, classes, and variables. Therefore, Modules help in organizing the code making it easier to use and understand. In addition, Modules provide the reusability of the code.

How to create a python modules

  • the import statement:

You can use any Python source file as a module by executing an import statement in some other Python source file.

example:

# Import module support

import support

# Now you can call defined function that module as follows

support.print_func(“Zara”)

output:

  • the from-import statement:

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

example:

Locating Modules

When you import a module, the Python interpreter searches for the module in the following sequences −

  • The current directory.
  • If the module isn’t found, therefore ,Python then searches each directory in the shell variable PYTHONPATH.
  • If all else fails, Python checks the default path. On UNIX, therefore, this default path is normally /usr/local/lib/python/.

The module search path is stored in the system module sys as the sys.pathvariable. therefore ,The sys.path variable contains the current directory, PYTHONPATH, and  in addition the installation-dependent default.

The dir( ) Function

The dir() built-in function returns a sorted list of strings containing the names defined by a module.

In addition ,the list contains the names of all the modules, variables and functions that are defined in a module .

example:

import math

content = dir(math)

print content

output:

Import modules as object

Python modules can be imported as objects. In such case, instead of 

module_name.function_name( )

 we use 

object.function_name( )

.

example:

>>> #importing findfact.py as f

 
>>> import findfact as f

 
>>> f.fact(5)

 
>>>120
 

>>> f.check_num(0)

>>>Number is zero.

Python modules search path

When a module is imported, Interpreter first searches for a built-in module with that name. If it is not found in the list of built-in modules, then the interpreter searched for the module in the following locations in order.

In addition to it,

  1. The current working directory.
  2. The directories of 
    PYTHONPATH

    . It is an environment variable with the list of directories.

  3. The standard installation path of Python – the installation dependent default.

Import everything a sa module

Besides importing certain functions, we can import everything defined inside the module and use the functions directly in the program.

example:

 

>>> #importing every functions from findfact.py

>>> from findfact import *

>>> check_num(2)

>>>2 is a positive number.

>>> fact(5)

>>>120

The reload() function

As we have already stated that, a module is loaded once regardless of the number of times it is imported into the python source file. However, if you want to reload the already imported module to re-execute the top-level code, python provides us the reload() function.

example :

Scope of variables

we have two types of scopes. All the variables defined in a module that have the global scope unless  it is define in a function.

All the variable define in a function have  a local scope that is limited to this function itself. We can not access a local variable globally.

If two variables are defined with the same name with the two different scopes, i.e., local and global, then the priority will always be given to the local variable.

Python packages

The packages in python facilitate the developer with the application development environment by providing a hierarchical directory structure where a package contains sub-packages, modules, and sub-modules. therefore ,The packages are  to categorize the application level code efficiently.

Let’s create a package named Employees in your home directory. Consider the following steps.

1. Create a directory with name Employees on path /home.

2. Create a python source file with name ITEmployees.py on the path /home/Employees.

ITEmployees.py

def getITNames():  

    List = [“John”“David”“Nick”,    “Martin”]  
    return List;

3. Similarly, create one more python file with name BPOEmployees.py and create a function getBPONames().

4. Now therefore, the directory Employees which we have created in the first step contains two python modules. To make this directory a package, we need to include one more file here, that is __init__.py which contains the import statements of the modules defined in this directory.

__init__.py

from ITEmployees import getITNames
from BPOEmployees import getBPONames

5. Now in addition , the directory Employees has become the package containing two python modules. Here in addition, we must notice that we must have to create __init__.py inside a directory to convert this directory to a package.

6. therefore ,To use the modules defined inside the package Employees, we must have to import this in our python source file.  therefore ,Let’s create a simple python source file at our home directory (/home) which uses the modules defined in this package.

Test.py

import Employees  
print(Employees.getNames())

Output:

in addition,We can have sub-packages inside the packages. We can nest the packages up to any level depending upon the application requirements.

https://javatpoint.com

 

%d bloggers like this: