A Python module consists Python source file. A Python module can comprises function, classes and statements.
Let us take a look at an example:
The above small code named module1.py, 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 module1.py.
Let us discuss in next example
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.
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:
The current directory, which contain the running script.
- PYTHONPATH (a list of directory names,the same syntax as we use in Unix and linux environment.
- The installation-dependent default.
If you want to know the search path you can check the sys.path
variable in sys
Compiled Python files
If a file called module1.pyc exists in the directory where module1.py 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 module1.py 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.
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.
A package is a Python module that contains other Python modules.
Let us discuss with example
Consider a file python.py
available in g:/prog directory
. This file has following line of source code.
print "I am Python Programer"
Similar way, we have another two files having different functions
print "I am Java developer"
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 __init__.py
from python import py
from linux import linux
from java import java
Now make final.py
which use these packages
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.
If you enjoyed the Python module page, like and share, if you have any suggestion do the comment.