Python has a way to put definitions in a file and use them in a script or in an interactive instance of the interpreter. Such a file is called a module; definitions from a module can be imported into other modules or into the main module (the collection of variables that you have access to in a script executed at the top level and in calculator mode).
A module is a file containing Python definitions and statements. The file name is the module name with the suffix
A module is a file containing Python definitions and statements. The file name is the module name with the suffix
.py appended. Within a module, the module’s name (as a string) is available as the value of the global variable __name__. A module can contain executable statements as well as function definitions. These statements are intended to initialize the module. They are executed only the first time the module name is encountered in an import statement. [1] (They are also run if the file is executed as a script.)
Each module has its own private symbol table, which is used as the global symbol table by all functions defined in the module. Thus, the author of a module can use global variables in the module without worrying about accidental clashes with a user’s global variables. On the other hand, if you know what you are doing you can touch a module’s global variables with the same notation used to refer to its functions,
modname.itemname.Modules can import other modules. It is customary but not required to place all
import statements at the beginning of a module (or script, for that matter). The imported module names are placed in the importing module’s global symbol table.There is a variant of the
import statement that imports names from a module directly into the importing module’s symbol table.When you run a Python module with
the code in the module will be executed, just as if you imported it, but with the
__name__ set to "__main__". That means that by adding this code at the end of your module:you can make the file usable as a script as well as an importable module, because the code that parses the command line only runs if the module is executed as the “main” file:
If the module is imported, the code is not run:
This is often used either to provide a convenient user interface to a module, or for testing purposes (running the module as a script executes a test suite).
When a module named
spam is imported, the interpreter first searches for a built-in module with that name. If not found, it then searches for a file named spam.py in a list of directories given by the variable sys.path. sys.path is initialized from these locationsAfter initialization, Python programs can modify
sys.path. The directory containing the script being run is placed at the beginning of the search path, ahead of the standard library path. This means that scripts in that directory will be loaded instead of modules of the same name in the library directory. The built-in function
dir() is used to find out which names a module defines. It returns a sorted list of stringsPackages are a way of structuring Python’s module namespace by using “dotted module names”. For example, the module name
A.B designates a submodule named B in a package named A. Just like the use of modules saves the authors of different modules from having to worry about each other’s global variable names, the use of dotted module names saves the authors of multi-module packages like NumPy or the Python Imaging Library from having to worry about each other’s module names.
Comments
Post a Comment