Module
======
sys,
os,
hashlib,
re,
optparse ................etc.
example.py, is called a module and its module name would be example.
We use modules to break down large programs into small manageable and
organized files. Furthermore, modules provide reusability of code. We
can define our most used functions in a module and import it, instead of
copying their definitions into different programs.example.py.
# Python Module example
def add(a, b):
"""This program adds two
numbers and return the result"""
result = a + b
return result
Here, we have defined a function add() inside a module named example. The function takes in two numbers and returns their sum.import keyword to do this. To import our previously defined module example we type the following in the Python prompt.>>> import exampleThis does not enter the names of the functions defined in
example directly in the current symbol table. It only enters the module name example there. Using the module name we can access the function using dot (.) operation. For example:>>> example.add(4,5.5) 9.5Python has a ton of standard modules available. You can check out the full list of Python standard modules and what they are for. These files are in the Lib directory inside the location where you installed Python. Standard modules can be imported the same way as we import our user-defined modules.
import statement and access the definitions inside it using the dot operator as described above. Here is an example.
# import statement example
# to import standard module math
import math
print("The value of pi is", math.pi)
OutputThe value of pi is 3.141592653589793
# import module by renaming it
import math as m
print("The value of pi is", m.pi)
The output of this is same as above. We have renamed the math module as m. This can save us typing time in some cases. Note that the name math is not recognized in our scope. Hence, math.pi is invalid, m.pi is the correct implementation.
# import only pi from math module
from math import pi
print("The value of pi is", pi)
The output of this is same as above. We imported only the attribute
pi form the module. In such case we don't use the dot operator. We could
have imported multiple attributes as follows.>>> from math import pi, e >>> pi 3.141592653589793 >>> e 2.718281828459045
# import all names form
# the standard module math
from math import *
print("The value of pi is", pi)
The output of this is same as above. We imported all the definitions
from the math module. This makes all names except those beginnig with an
underscore, visible in our scope.sys.path. The search is in this order.PYTHONPATH (an environment variable with a list of directory).>>> import sys >>> sys.path ['', 'C:\\Python33\\Lib\\idlelib', 'C:\\Windows\\system32\\python33.zip', 'C:\\Python33\\DLLs', 'C:\\Python33\\lib', 'C:\\Python33', 'C:\\Python33\\lib\\site-packages']We can add modify this list to add our own path.
my_module.
# This module shows the effect of
# multiple imports and reload
print("This code got executed")
Now we see the effect of multiple imports.>>> import my_module This code got executed >>> import my_module >>> import my_moduleWe can see that our code got executed only once. This goes to say that our module was imported only once.
reload() function inside the imp module to reload a module. This is how its done.>>> import imp >>> import my_module This code got executed >>> import my_module >>> imp.reload(my_module) This code got executed <module 'my_module' from '.\\my_module.py'>
dir() function to find out names that are defined inside a module. For example, we have defined a function add() in the module example that we had in the beginning.>>> dir(example) ['__builtins__', '__cached__', '__doc__', '__file__', '__initializing__', '__loader__', '__name__', '__package__', 'add']Here, we can see a sorted list of names (along with
add).
All other names that begin with an underscore are default Python
attributes associated with the module (we did not define them ourself).
For example, the __name__ attribute contains the name of the module.>>> import example >>> example.__name__ 'example'All the names defined in our current namespace can be found out using the
dir() function without any arguments.>>> a = 1 >>> b = "hello" >>> import math >>> dir() ['__builtins__', '__doc__', '__name__', 'a', 'b', 'math', 'pyscripter']