Python Modules

A python module can be represented as a python program file which includes a python code including python functions, class, or variables. In other terms, we can say that our python code file saved with the extension (.py) is treated as the module. We may have a runnable code inside the python module.

Python Modules allows us to logically organize our Python code which makes the code easier to understand and use. In Python language, a module is just a file consisting of Python code which has several in-built or user-defined functions.

Python import Statement

We can call the Python code or functions present in a file as a module by using the import statement followed by the Python file name. Following is the syntax for the import statement in Python.

import module1[, module2[,… moduleN]

Example

Creating a Python file to be used as a module.

python-modules

Calling the above module and operating on the user-defined function in the current Python program is shown in the below example.

python-modules
Python from…import Statement

In Python language, from…import statement lets us import only specific and not all attributes from a module into the current namespace.

It has the following syntax:

from modulename import name1[, name2[, … nameN]]

For example, if we need to import only the function addition from the above module ReturnStatement, then we use the following statement − from ReturnStatement import addition.

This statement does not import the entire module ReturnStatement into the current namespace, but it just introduces the function addition from the module ReturnStatement into the global symbol table of the importing module. This is demonstrated in the below example.

python-modules
Python from…import* Statement

Using this Python statement, we can import all names or functions from a module into the current namespace.

It has the following syntax:

from modulename import *

Although, it provides an easy way to import everything from a module into the current namespace, yet this statement is used rarely.

Locating Python Modules

In Python, when we import a module, the Python interpreter searches for the module in the following sequences. First in the current directory, if that module isn’t located then Python searches each directory in the shell variable PYTHONPATH. If it fails here as well, then lastly Python checks the default path.

Search path for the module is stored in the system module sys as the sys.path variable. The sys.path variable contains the current directory, PYTHONPATH, and the installation dependent default.

The PYTHONPATH Variable:

The PYTHONPATH is an environment variable that consists of a list of directories. Below are the syntaxes of PYTHONPATH for Windows and UNIX.

PYTHONPATH from a Windows system:

set PYTHONPATH=c:\Python34\lib;

PYTHONPATH from a UNIX system:

set PYTHONPATH=/usr/local/lib/python

Python in-built functions for Modules

Following are the in-built Python functions that are used while working with modules.

Functions Descriptions
dir ( ) The dir () Python built-in function returns a sorted list of strings containing the names defined by a module. E.g. if the module name is math, then dir (math) will list down all the names present in that module.
globals () If Python in-built function globals () is called from within a function, then it will return all the names (as dictionary datatype) that can be accessed globally from that function.
locals() If Python in-built function locals () is called from within a function, then it will return all the names (as dictionary datatype) that can be accessed locally from that function.
reload() In Python, when we import a module into a script, the code in the top-level portion of a module is executed only once. Therefore, if we want to execute the top-level code in a module again,then we have to use the reload () function.This function imports a previously imported module again.
Python Math Module

Python math module is represented as the most popular mathematical functions, which contains trigonometric functions, representation functions, logarithmic functions, etc. It can also describe two mathematical constants includes Pie and Euler number, etc.

Pie (n): It is a well-known mathematical constant and defined as the ratio of circumstance to the diameter of a circle. Its value is 3.141592653589793.
Euler’s number(e): It is represented as the base of the natural logarithmic, and its value is 2.718281828459045.

Enroll Yourself: Python Training

Copyright 1999- Ducat Creative, All rights reserved.