Python Module Searching and Invocation
This article will explain where the modules are kept and what Python commands are used to run them.
modules.
Locating the Modules in Python
The Python interpreter will first look at the current directory when a user imports a module. If the module is not present in the current directory, the interpreter will look in all directories listed in the PYTHONPATH shell variable. The interpreter will try the default path if it can’t find it in the shell. The standard location for Python libraries on UNIX is /usr/local/lib/python/.
The sys.path variable in the system module sys stores the module’s search route. The current working directory, or PYTHONPATH, and the installation-specific
default.
The PYTHONPATH Variable
The PYTHONPATH variable is a platform-specific environment variable that stores a path to the Python installation directory. It matches the format of the shell variable PATH.
PYTHONPATH outside the panes
system:
-
set
PYTHONPATH
=
c
: \\python3\\lib;
The UNIX PYTHONPATH
system
-
set
PYTHONPATH
= /usr/local/lib/python
Executing Modules in Python
The -m option in the command line interprets the path to the specified module and runs it as the __main__ module. This technique is supported internally by the runpy module, which is a standard Python module. With the runpy module, you can search for a script within the Python module’s namespace rather than the filesystem.
Two are defined in the runpy module:
functions:
- run_module()
- run_path()
run_module()
The run_module() function is used to run the code that contains the module in question, and it returns the module’s globals dictionary as its result.
The module’s actual name should be used for the module_name argument. What if, instead of a standard module, any package with the same name was used? As soon as that package is imported, its __main__ submodule is performed, and the result of the module globals dictionary is returned.
Before a module is executed, the globals dictionary is initialised with values for the special global variables __name__, __spec__, __file__, __cached__, __loader__, and __package__.
If the identified module is a package, its __name__ will be set to module_name +’.__main__’; otherwise, it will be set to the module_name argument.
And the module-specific defaults for __file__, __cached__, __loader__, and __package__ are used.
spec.
run_path()
The run_path() function executes the code located at the specified path and returns a dictionary containing the module’s global variables. Python source code, compiled bytecode, or a valid sys.path entry containing the __main__ module (such as a zipfile containing the top-level __main__.py file) can all be referenced from the supplied path.
Before a module is executed, the globals dictionary is initialised with values for the special global variables __name__, __spec__, __file__, __cached__, __loader__, and __package__.
If this optional argument is not None, then the __name__ variable will be set to run_name; otherwise, it will be set to run_path>. Example:
An illustration of a runpy module:
Initiate the script by renaming the following file:
runpy_example.py.
-
def add(p, q, r, s, t):
-
return p + q + r + s + t
-
def main():
-
p
=
4
-
q
=
6
-
r
=
2
-
s
=
8
-
t
=
7
-
print (“sum of p, q, r, s,
t
= “)
-
print (add(p,q,r,s,t))
-
return
-
if
__name__
==’__main__’:
-
main()
The aforementioned file will then be run by the user via
command:
Resulting
-
import runpy_example as runp
-
runp.main()
sum of p, q, r, s, t = 27
The aforementioned file can be run without the need for import in most cases.
it:
Final Product:
-
import runpy
-
runpy.run_module(‘runpy_example’,
run_name
=
‘__main__’
)
sum of p, q, r, s, t = 27
The run_path option is also available.()
function:
Resulting
-
runpy.run_path(‘runpy_example.py’,
run_name
=
‘__main__’
)
sum of p, q, r, s, t = 27
As was previously mentioned, the runpy is compatible with Python’s -m option.
line:
-
C:\python37
>
python -m runpy_example