Modules are a core concept of the Python program architecture, and larger programs are often presented as multiple module files, and a module is designed as a master file or a top-level file to launch the entire Python program. Each python source code file with a. py suffix is a module, and other files can read the contents of this module through "import". In a general sense, a module is a package of variable names. such as writing a module test.py, contains a two variable name name, age.
Name= ' Aidan ' age=27
Then, by executing the following command in the Python command line
Import Testprint (Test.name, Test.age)
Can get results: Aidan 27
The function dir (modelname) can be used to get the variable names that are available inside the module, including some python-built variable names such as ' __doc__, __file__ '.
A python program is often made up of multiple modules, connected via import. Each module file is a namespace and cannot see the variable names of other module files unless the file is imported through import or a variable is imported through the From model import varible. This avoids the conflict of variable naming, because each module is a separate namespace, similar to a function in the C language and its local variables.
In essence, "import" is to load the contents of another file in one file, so that another file can be used in the outside world, and the directive is import name.py. Import is only effective for the first execution of each session, and subsequent import of the same file multiple times is not valid, even if the file has changed because the file was compiled into bytecode the first time it was imported. The import module must know the detailed path of the module (file search, which can be used to indicate all directories that need to be searched through the Pythonpath variable in sys.path), so in order to be simple, all the files that need to be imported are placed in the same directory.
If you want to run the same file multiple times in the same session (or the file changes and must be overloaded), you need to call the "overloaded" function-reload (name) and make sure that the module has been successfully imported through import before calling the reload function. See the difference between "function" reload () and "statement" import, Reload () is a function, parameter is the file module filename that was imported, import is a statement, no parentheses are required. Python's overloaded functionality allows the user to edit and improve the code module during the interaction, so in order to ensure that the most current code is running, first use reload ().
Python Learning Notes (iii) Python module, module import and reload