Python Programming Modules and Packages

What is module in python?

  • Pyhton Module is a python script file containing python definitions (functions and variables) and statements.
  • Python script file name is the module name with the suffix .py appended
  • Definitions from a module can be imported into other modules or into the main module.
  • Python module can be used in python script or in an interactive mode of python interpreter.
  • A module can contain executable statements as well as function definitions.
  • In python 3, Global variable __name__ is available in all the modules for holding value module's name as string.
    $ python3
    Python 3.5.3 (default, May 11 2017, 09:10:41) 
    [GCC 6.3.1 20161221 (Red Hat 6.3.1-1)] on linux
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import arithmetic
    >>> arithmetic.__name__
    'arithmetic'
    >>> 
    
    here arithmetic is the python module name.

What are the python module advantages?

  • We can split longer python program into several python files as categorized for easier maintenance.
  • Avoids code duplication like copying same function in multiple python program (code reusability).

Creating python module

Create python script file as arithmetic.py
def sum(a, b):                                                                  
    return a+b;                                                                 
                                                                                
def sub(a, b):                                                                  
    return a-b;                                                                 
                                                                                
def mul(a, b):                                                                  
    return a*b;                                                                 
                                                                                
def div(a, b):                                                                  
    return a/b; 

Testing created module in python interactive mode

$ python
Python 2.7.13 (default, May 10 2017, 20:04:36) 
[GCC 6.3.1 20161221 (Red Hat 6.3.1-1)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import arithmetic
>>> arithmetic.sum(6, 4)
10
>>> arithmetic.sub(6, 4)
2
>>> arithmetic.mul(6, 4)
24
>>> arithmetic.div(6, 4)
1
>>> exit()

Import module in python

  • Any python script file can be imported as module in another python script file using import statement.
  • Searches the python module in the current directory and search path. Search path is a list of directories that the interpreter searches to import a module.
  • Each module is imported only once even though multiple import statements for same module used in python script file.
  • Use importlib.reload() method to reload only once module again without restarting interpreter.
    import importlib; importlib.reload(modulename)
    
    Need to restart the interpreter if you change the modules.
import module1[, module2, module3..]

Importing created module in another python file

import arithmetic;                                                              
                                                                                
print "sum of 6 and 5: ", arithmetic.sum(6,4);                                  
print "sub of 6 and 5: ", arithmetic.sub(6,4);                                  
print "mul of 6 and 5: ", arithmetic.mul(6,4);                                  
print "div of 6 and 5: ", arithmetic.div(6,4);   
Output:
$ python test_module.py
sum of 6 and 5:  10
sub of 6 and 5:  2
mul of 6 and 5:  24
div of 6 and 5:  1

from import statement

from import statement is used to import names from a module directly.
//arithmetic.py
def add(a, b):                                                                  
    return a+b;                                                                 
                                                                                
def sub(a, b):                                                                  
    return a-b;                                                                 
                                                                                
def mul(a, b):                                                                  
    return a*b;                                                                 
                                                                                
def div(a, b):                                                                  
    return a/b;                                                                 
//test_module.py
from arithmetic import add, sub;                                                
                                                                                
print "sum of 6 and 5: ", add(6,4);                                             
print "sub of 6 and 5: ", sub(6,4);  
Output:
$ python test_module.py
sum of 6 and 5:  10
sub of 6 and 5:  2
if you try to invoke arithmetic.mul(6, 4), raises below error
Traceback (most recent call last):
  File "test_module.py", line 5, in 
    print "mul of 6 and 5: ", mul(6,4); 
NameError: name 'mul' is not defined

from import * statement

import all names that a module defines
from arithmetic import *;                                                       
                                                                                
print "sum of 6 and 5: ", add(6,4);                                             
print "sub of 6 and 5: ", sub(6,4);                                             
print "mul of 6 and 5: ", mul(6,4);                                             
print "div of 6 and 5: ", div(6,4);   
Output:
$ python test_module.py
sum of 6 and 5:  10
sub of 6 and 5:  2
mul of 6 and 5:  24
div of 6 and 5:  1

Executing python module

We can run a Python module with
if __name__ == "__main__":                                                      
    import sys;                                                                 
    print "sum: ", add(int(sys.argv[1]), int(sys.argv[2]));  
// arithmetic.py
def add(a, b):                                                                  
    return a+b;                                                                 
                                                                                
def sub(a, b):                                                                  
    return a-b;                                                                 
                                                                                
def mul(a, b):                                                                  
    return a*b;                                                                 
                                                                                
def div(a, b):                                                                  
    return a/b;                                                                 
                                                                                
if __name__ == "__main__":                                                      
    import sys;                                                                 
    print "sum: ", add(int(sys.argv[1]), int(sys.argv[2]));   
Output:
$ python arithmetic.py 6 4
sum:  10

Module Search Path

sys.path: List of strings that specifies the search path for modules. Initialized from the environment variable PYTHONPATH, and an installation-dependent default. sys.path is the module search path and initializes from these locations.
  • Directory is containing the input script (or current directory)
  • PYTHONPATH (directories, same as shell variable PATH).

Compiled python modules

  • Compiled python files are used to speed up loading modules, python caches each module compiled version in the __pycache__ directory under the name of file as module.version.pyc which contains the Python version number.
  • Module compileall can create .pyc files for all modules in a directory.
  • -O or -OO switches on the Python command can be used to reduce the size of a compiled module.
  • -O switch on python command removes only assert statements.
  • -OO switch on python command removes both assert statements and __doc__ strings.
  • program doesn’t run any faster when it is read from a .pyc file than read from a .py file. only thing that’s faster about loading modules when reading from .pyc files.
  • dir method
  • Built-in dir function is used to sorted list names from module defines.
    $ python
    Python 2.7.13 (default, May 10 2017, 20:04:36) 
    [GCC 6.3.1 20161221 (Red Hat 6.3.1-1)] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import arithmetic, sys
    >>> dir(arithmetic)
    ['__builtins__', '__doc__', '__file__', '__name__', '__package__', 'add', 'div', 'mul', 'sub']
    >>> dir(sys)
    ['__displayhook__', '__doc__', '__excepthook__', '__name__', '__package__', '__stderr__', '__stdin__', '__stdout__', '_clear_type_cache', '_current_frames', '_debugmallocstats', '_getframe', '_mercurial', 'api_version', 'argv', 'builtin_module_names', 'byteorder', 'call_tracing', 'callstats', 'copyright', 'displayhook', 'dont_write_bytecode', 'exc_clear', 'exc_info', 'exc_type', 'excepthook', 'exec_prefix', 'executable', 'exit', 'flags', 'float_info', 'float_repr_style', 'getcheckinterval', 'getdefaultencoding', 'getdlopenflags', 'getfilesystemencoding', 'getprofile', 'getrecursionlimit', 'getrefcount', 'getsizeof', 'gettrace', 'hexversion', 'long_info', 'maxint', 'maxsize', 'maxunicode', 'meta_path', 'modules', 'path', 'path_hooks', 'path_importer_cache', 'platform', 'prefix', 'ps1', 'ps2', 'py3kwarning', 'pydebug', 'setcheckinterval', 'setdlopenflags', 'setprofile', 'setrecursionlimit', 'settrace', 'stderr', 'stdin', 'stdout', 'subversion', 'version', 'version_info', 'warnoptions']
    >>> dir()
    ['__builtins__', '__doc__', '__name__', '__package__', 'arithmetic', 'sys']
    
    dir() method returns currently defined names list, not lists built-in functions and variables.

    Python Packages

    Packages are used to structure python’s module like namespace by using “dotted module names”. Example
    A.B   # sub module B in a package A.
    
    B is the sub module in package A.

    Importing package

    imports add sub module in math.arithmetic module.
    import math.arithmetic.add
    
    imports all the sub modules of math.arithmetic module.
    from math.arithmetic import *
    
    relative reference import, imports add sub module from current module.
    from . import add
    from .. import add
    

    Privacy Policy  |  Copyright@2017 - All Rights Reserved.  |  Contact us   |  Report website issues in Github   |  Facebook page   |  Google+ page

    Free online programming tutorials

    Email Facebook Google LinkedIn Twitter
    ^