Python Modules

A Python module consists Python source file. A Python module can comprises function, classes and statements.
Let us take a look at an example:
def sum(a,b):
   c =a+b
   print c
def mul(a,b):
   c =a*b
   print c
The above small code named, consider it as a Python module.

The import Statement

The import has the following syntax:
import module1, module2, module 
The import statement should be in the starting of code. The import keyword is followed by one or more Python module specifies, separated by commas. When interpreter see the import statement it import the module if avaliable. For example, to import the module
Let us discuss in next example
import module1
a =10
b =9
k = module1.sum(a,b)
Python interpreter execute module body immediately. A Python module is loaded only once, In order to access attribute of module, use module object as prefix. In above example module1 is the module name and function sum() is function defined in module.
Python module

The from...import Statement

Python's from statement allows you to import specific attributes from a module into the current program. Syntax of from import statement.
 from module_name import name1,  name2 
You can import all names from a module, use following statement.
 from module_name import * 

Locating Python Modules

After encounter import statement Python interpreter seraches the module in the following sequences:
  1. The current directory, which contain the running script.
  2. PYTHONPATH (a list of directory names,the same syntax as we use in Unix and linux environment.
  3. The installation-dependent default.
If you want to know the search path you can check the sys.path variable in sys module.
Python sys.path
Showing sys.path

Compiled Python files

If a file called module1.pyc exists in the directory where is found as shown in figure below, this is assumed to contain an already-"byte-compiled" version of the module module1. Normally, you don't need to do anything to create the module1.pyc file. Whenever is successfully compiled, an attempt is made to write the compiled version to module1.pyc. It is not an error if this attempt fails; if for any reason the file is not written completely, the resulting module1.pyc file will be recognized as invalid and thus ignored later. The contents of the module1.pyc file are platform independent, so a Python module directory can be shared by machines of different architectures.
Python complied File
Showing compiled file


The built-in function dir() is used to find out which names a module defines. It returns a sorted list of strings.
import module1
print dir(module1)
Directory of module

Python Package

A package is a Python module that contains other Python modules.
Let us discuss with example
Consider a file available in g:/prog directory . This file has following line of source code.
def py():
   print "I am Python Programer"
Similar way, we have another two files having different functions
def java():
   print "I am Java developer"

def linux():
   print "I am Linux Admin"

To make all of your functions available when you've imported prog , you need to put explicit import statements in as follows:
from python import py
from linux import linux
from java import java

Now make which use these packages
import prog

When you excute the above file it'would give error because python interpreter don't know the g:/prog path you have to set the path using command as written below
set PYTHONPATH = g:/prog
Below figure showing the successful execution.
Python packages
Python packages
If you enjoyed the Python module page, like and share, if you have any suggestion do the comment.