Python Programming – Python Modules
In the previous chapter, we have learned about the creation of Python user-defined functions. We have seen that a large problem can be divided into small fragments to solve each of them separately using functions. In this chapter, we study modules, which are an extension to the user-defined functions. As in C/C++, and Java header files or packages are used to make use of built-in functions, in Python modules are used to utilize built-in Python functions. Apart from that, similar to C/C++/Java, where users can create header files or packages to use them similar to built-in ones, Python has the feature to create modules for further use.
Need for Module
In the interpreter-based Python programming language, all the definitions, variables, and functions written by the programmer get lost, when he quits the interpreter. However, another way to save the program code is to write the program in a script form in some editor such as notepad, which we have followed so far in this book. For large programs, it is a good practice to divide it into several files for better understanding and maintenance. Moreover, sometimes a similar function is used in multiple programs, so rather than writing the same code again and again it is a good practice to make it a module and use it in different programs.
To support this, Python has the distinct feature of the module in which a programmer can put definitions in a file and use them in a script or in an interactive instance of the interpreter. The definitions written inside a module can be imported into the main module by using the import command. Another major role of the module is to provide the reusability of code. As most used function definitions can be written inside a module and be used several times without writing similar code repetitively.
- Python Programming – Types Of Functions
- Python Programming – Python Functions
- Python Programming – Python User Defined Functions
A module can be defined with some function definitions and statements. The file name is the module name or it can be the same as the task it performs with an extension .py. For example, if we wish to design a module that computes factorial then its name can be factorial.py. It is to be noted that the module file must be saved in the same directory, where the main program resides or where it will be imported.
Importing Module in the Interpreter
The module is given in Code 7.1. is saved as fact.py in the same directory. where Python interpreter is located. Now, in the interpreter mode we just import the fact module and then make a call to factorial) to achieve the operation. The interpreter mode is given in Fig. 7.1. We see from the figure that factorial) is called four times after importing the fact module and provides the intended output.
Fig. 7.1. The output of importing and calling fact module in interpreter mode.
From the above sections, we see that the statements defined inside a module can be imported inside another module or at the Python prompt of the interactive Python interpreter. The import statement is used to achieve this task, ‘import’ is a keyword followed by module name to be imported. In the above programming examples, we import the fact module at Python prompt as follows
|>>> import fact|
The above statement does not include all the functions defined inside the fact module. However, it only inserts the module name. This imported or inserted module then can be used to call any function defined inside its script using the dot(.) operator. For example, the function factorial( ) can be called from the module fact as follows:
|>>> fact, factorial (10)|
A particular function definition or a name from a module can be called using the import statement. In Code 7.3., we see that two function definitions fib() and fiber( ) have been defined in the script of module Fibonacci. A specific function let’s assume floor( ) can be imported as follows
|import fibo( ) from fibonacci|
By using the above statement only the fibo() function is imported from the Fibonacci module. Thus, in the calling script the programmer needs to write only flbo(n) rather than Fibonacci.flbo(n) statement as shown in Code: 7.5. One can import all the function definitions of a module by using the * (asterisk) operator as follows
|import * from fibonacci|
By using the above statement, both fib() and fibo() functions are imported in the module Fibonacci. Thus, user can access their definitions directly without using the dot(.) operator. However, for good programming practice, importing everything with the asterisk (*) symbol should be avoided. This can lead to duplicate definitions for an identifier. It also impedes the readability of the code.
Search Path of Module
When a module is imported either in interactive mode or script mode, the Python searches the path of the module at various places. The interpreter initially looks for the built-in modules. In the case of user-created modules, the search order of the interpreter works as follows:
- The current directory
- The path set in the environment variable (PYTHONPATH)
- The default directory sys. path
The Python interpreter imports a module only once in a session. If some modifications are performed in the module script then it must be reloaded (imported) again in the interpreter for further use. This can be illustrated through an example. Consider Fig. 7.1., where the module fact has been imported to call the factorial() function. If the programmer makes some changes in the definition of factorial() then the fact module needs to be imported again in the interpreter otherwise it will provide the last executed output. One way is to call the import command again on the prompt as import fact or restart the interpreter. The other way is to call the reload( ) function, which is defined inside the imp module. This can be done as follows
|>>> import imp
The dir ( ) Function
In order to see the list of function names defined in a module, Python is provided with a built-in function called dir(). It displays the list of all the function definition names as follows
[‘__builtins__ ‘ __cached__ ‘ __doc__ ‘ __file__ ‘ __loader__’ ‘ __name__ ‘ __package__’ ‘__spec __’/fib’]
From above, we see a sorted list of names with the user-created definition fib is displayed as the output. The names begin with an underscore _ are default Python attributes with the module. They are associated by themselves with Python while creating a module. By calling empty dir() results as follows
[‘__builtins__’, ‘__cached__’, ‘__doc__’, ‘__file__’, ‘__loader__’,’__name__’, ‘__package__’, ‘__spec__’, ‘__fibonacci__’]
It shows that it provides the module name Fibonacci with the built-in files rather than providing the function definition names as no module name is passed as an argument to dir().
The Python language comprises a library of standard modules. Some of them are available on the interpreter, which provides access to operations that are not part of the core of the language. Such modules are used to make system calls or to provide access to the operating system. One of the important system module sys is available on Python interpreter. The variable sys. the path is a list of strings that determines the search path for modules. Usually, it is initialized to the default path taken from the environment variable PYTHONPATH. This can be modified by using standard list operations. The list of built-in standard modules can be seen in Appendix-I.