Locating Modules in Python with code examples: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:\python27\lib;And here is a typical PYTHONPATH from a UNIX system:
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 access variables in a local namespace and in the 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 Value in the global namespace. Within the function Value, we assign Value a value, therefore Python assumes Value as a local variable. However, we accessed the value of the local variable Value before setting it, so an UnboundLocalError is the result. Uncommenting the global statement fixes the problem.
Value = 2000 def AddValue(): # Uncomment the following line to fix the code: # global Value Value = Value + 1 print Value< AddValue() print Value
Python dir( ) Function:
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 −
# Import built-in module math import math content = dir(math) print contentAfter executing the above code the result is shown below:
['__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']NOTE:Here, the special string variable __name__ is the module's name, and __file__ is the filename from which the module was loaded.
The global() and locals() Functions in python:
The global() function is used to return the names in the global namespace depends on the location where they are called.
This function is called from within a function; it will return all the names that can be accessed globally from that function. The return type is dictionary. So the name can be extracted using keys () function.
The locals () function is used to return the names in the local namespace depending on the location from where they are called.
This function is called from within a function; it will return all the names that can be accessed locally from that function. The return type is dictionary. So the name can be extracted using keys() function.
The reload () Function in Python
When the module is imported into a script, the code in the top-level portion of a module is executed only once.
This is used to reexecute the top-level code in a module, we can use the reload () function. The reload () function imports a previously imported module again.
The following is the syntax of the reload () function
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 Test module, do the following −