Module and
File
27/06/2018
Workshop on “Introduction to
Python
Department Of
Computer Science
Engineering
SKFGI, Mankundu
, 1.Modules
Sub Topics –
Importing Module
Math Module
Random Module
Namespace and DIR()
Packages
Composition
1.1.Why Modules?
If you quit from the Python interpreter and enter it again, the definitions
you have made (functions and variables) are lost. Therefore, if you want
to write a somewhat longer program, you are better off using a text editor
to prepare the input for the interpreter and running it with that file as
input instead. This is known as creating a script. As your program gets
longer, you may want to split it into several files for easier maintenance.
You may also want to use a handy function that you‟ve written in several
programs without copying its definition into each program.
To support this, Python has a way to put definitions in a file and use them
in a script or in an interactive instance of the interpreter. Such a file is
called a module; definitions from a module can be imported into other
modules or into the main module (the collection of variables that you
have access to in a script executed at the top level and in calculator
mode).
A module allows you to logically organize your Python code. Grouping
related code into a module makes the code easier to understand and use.
A module is a Python object with arbitrarily named attributes that you can
bind and reference.
, 1.2.Module Loading and Execution:
A module imported in a program must be located and loaded into memory
before it can be used. 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, Python then searches each directory in
the shell variable PYTHONPATH.
If all else fails, Python checks the default installation specific path.
On UNIX, this default path is normally /usr/local/lib/python/.
If the module is not located even there then an error ImportError
exception is generated .
A compiled version of the module with file extension .pyc is generated .
next time when the module is imported , this .pyc file is loaded rather
than .py file, to save the time of recompiling. A new compiled version of a
module is again produced whenever the compiled version is out of date.
Even programmer can force the python shell to reload and recompile the
.py file to generate a new .pyc file by using reload function.
1.3.The import Statement
We can use any Python source file as a module by executing an import
statement in some other Python source file. The import has the following
syntax −
import module1[, module2[,... moduleN]
import sys
print(sys.path)
Output:
['', 'C:\\Users\\PC\\AppData\\Local\\Programs\\Python\\Python36-
32\\Lib\\idlelib',
'C:\\Users\\PC\\AppData\\Local\\Programs\\Python\\Python36-
32\\python36.zip',
'C:\\Users\\PC\\AppData\\Local\\Programs\\Python\\Python36-
32\\DLLs',
'C:\\Users\\PC\\AppData\\Local\\Programs\\Python\\Python36-32\\lib',
'C:\\Users\\PC\\AppData\\Local\\Programs\\Python\\Python36-32',