A module authorizes users logically from your Python code. Grouping considering code into a module brand the code easier to realize and use. A module is a Python object with randomly named attributes that you can bind and reference.
Merely, a module is a file comprising Python code. A module can state functions, sets, and variables. A unit can also slot in running code.
The Python code for a module named name normally resides in a file named aname.py. Here's an example of a simple module, support.py
def print_func( par ): print "Hello : ", par return
You can usage 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]
When the interpreter encounters an import statement, it imports the module if the module is presented in the search path. A search path is a list of directories that the interpreter searches before importing a module. For example, to import the module support.py, you need to put the following command at the top of the script:
#!/usr/bin/python # Import module support import support # Now you can call defined function that module as follows support.print_func("Zara")
When the above code is executed, it produces the following result:
Hello : Zara
A module is loaded only once, respective of the number of times it is imported. This forecloses the module execution from happening over and over again if multiple imports occur.
Python's from statement enables you to import specific attributes from a module into the current namespace. The from...import has the following syntax:
from modname import name1[, name2[, ... nameN]]
For example, to import the function Fibonacci from the module fib, use the following statement:
from fib import fibonacci
This statement does not import the entire module fib into the current namespace; it just introduces the item Fibonacci from the module fib into the global symbol table of the importing module.
It is also possible to import all names from a module into the current namespace by using the following import statement:
from modname import *
This provides an easy way to import all the items from a module into the current namespace; however, this statement should be used sparingly.
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 path. On UNIX, this default path is normally /usr/local/lib/python/.
The module search path is stored in the system module sys as the sys.path variable. The sys.path variable contains the current directory, PYTHONPATH, and the installation-dependent default.
The PYTHONPATH is an environment variable, consisting of a list of directories. The syntax of PYTHONPATH is the same as that of the shell variable PATH.
Here is a typical PYTHONPATH from a Windows system:
set PYTHONPATH = c:\python20\lib;
And here is a typical PYTHONPATH from a UNIX system:
set PYTHONPATH = /usr/local/lib/python
Variables are names (identifiers) that map to objects. A namespace is a dictionary of variable names (keys) and their corresponding objects (values).
A Python statement can approach variables in a local namespace and in a global namespace. If a local and a global variable have the same name, the local variable shadows the global variable.
Each function has its own local namespace. Class methods follow the same scoping rule as ordinary functions.
Python makes educated guesses on whether variables are local or global. It assumes that any variable assigned a value in a function is local.
Therefore, in order to assign a value to a global variable within a function, you must first use the global statement.
The statement global VarName tells Python that VarName is a global variable. Python stops searching the local namespace for the variable.
For example, we define a variable Money in the global namespace. Within the function Money, we assign Money a value, therefore Python assumes Money as a local variable. However, we accessed the value of the local variable Money before setting it, so an UnboundLocalError is a result. Uncommenting the global statement fixes the problem.
#!/usr/bin/python Money = 2000 def AddMoney(): # Uncomment the following line to fix the code: # global Money Money = Money + 1 print Money AddMoney() print Money
The dir() built-in function returns a sorted list of strings containing the names defined by a module.
The list contains the names of all the modules, variables, and functions that are defined in a module. Following is a simple example:
#!/usr/bin/python # Import built-in module math import math content = dir(math) print content
When the above code is executed, it produces the following result:
['__doc__', '__file__', '__name__', 'acos', 'asin', 'atan', 'atan2', 'ceil', 'cos', 'cosh', 'degrees', 'e', 'exp', 'fabs', 'floor', 'fmod', 'frexp', 'hypot', 'ldexp', 'log', 'log10', 'modf', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh']
Here, the special string variable __name__ is the module's name, and __file__ is the filename from which the module was loaded.
The globals() and locals() functions can be used to return the names in the global and local namespaces depending on the location from where they are called.
If locals() is called from within a function, it will return all the names that can be accessed locally from that function.
If globals() is called from within a function, it will return all the names that can be accessed globally from that function.
The return type of both these functions is a dictionary. Therefore, names can be extracted using the keys() function.
When the module is imported into a script, the code in the top-level portion of a module is executed only once.
Therefore, if you want to re-execute the top-level code in a module, you can use the reload() function. The reload() function imports a previously imported module again. The syntax of the reload() function is this:
reload(module_name)
Here, module_name is the name of the module you want to reload and not the string containing the module name. For example, to reload the hello module, do the following:
reload(hello)
A package is a hierarchical file directory structure that defines a single Python application environment that consists of modules and sub-packages and sub-sub packages, and so on. Consider a file Pots.py available in the Phone directory. This file has the following line of source code:
#!/usr/bin/python def Pots(): print "I'm Pots Phone"
Similar way, we have another two files having different functions with the same name as above:
Now, create one more file __init__.py in the Phone directory:
To make all of your functions available when you've imported Phone, you need to put explicit import statements in __init__.py as follows:
from Pots import Pots from Isdn import Isdn from G3 import G3
After you add these lines to __init__.py, you have all of these classes available when you import the Phone package.
#!/usr/bin/python # Now import your Phone Package. import Phone Phone.Pots() Phone.Isdn() Phone.G3()
When the above code is executed, it produces the following result:
I'm Pots Phone I'm 3G Phone I'm ISDN Phone
In the aforementioned instance, we have full examples of single functions in each file, except you can stay multiple functions in users' files. You can also describe different Python classes in those files and then you can generate your packages out of those classes.
Here at Intellinuts, we have created a complete Python tutorial for Beginners to get started in Python.