I. The nature of the moduleWe write Python programs that often import many modules, what exactly is a module? In fact, the module is a python file, we import the module name is the name of the Python file, for example, we wrote a file called my_module.py can be used as a module to import other Python files, the module name is My_module. two modes of module import2.1. ImportThere are three things to do when importing modules 2.1.1 and import:1. Generate a separate namespace for the module2. Execute the module's code again in this namespace3. Establish a reference to the module name to that namespace 2.1.2, aliasesWhat do we do when we import a module name that is too long to call, or has the same name as our own defined function or variable name? At this point, you can use the AS keyword to give the module an individual name, and then use that alias when you take a function or variable in the module, such as
Import Random as R
Print (R.randint (1,5))
4
2.1.3, importing multiple modules in one linepython supports import followed by multiple module names, separated by commas
Import Random, Os,sys
2.2. From importthe difference between 2.2.1 and importThe From import also creates a separate namespace for the module, but it imports the import name directly into the current namespace, and we can use the name directly without the module name. The way the name is, like
From collections Import namedtuplepoint=nametuple (' point ', [' X ', ' y ']) p=point (P.X,P.Y)------------------- -------------------------------------------------------------------1 2
However, it should be noted that if there is a case with the name of the import, the imported name will be overwritten by the subsequent 2.2.2, aliasesThe from import also supports using the AS keyword to alias the imported name, as well as using the alias for subsequent use. 2.2.3, importing multiple names of a module at the same timethe From import supports importing multiple names in a row, such as
From collections Import namedtuple,ordereddict,defaultdict,deque,counterpoint=namedtuple (' point ', [' X ', ' y ']) p= Point Print (P.X,P.Y)-------------------------------------------------------------------------------------- 1 2
2.2.4, from import *From Import * You can import all the names of the modules except the ones that start with the underscore _ into the current namespace, and we can use __all__=[' name 1 ', ' Name 2 ' to control which names can be imported, but in most cases it is not recommended to import them. Because we don't know what the name is in the module, it will be the same as the name in the existing code. third, module reloadPython does not support reloading modules or unloading modules that have already been loaded, so once the program is run, modifications to the module will not take effect, normally only restart the program, but there is a way to reload the module when the program is running, is the reload method of the Importlib module, after importing the importlib module, we can reload the module using the Importlib.reload (module name), but it is only recommended for testing. Iv. execution Logic of the control moduleThere is a __name__ variable in the Python built-in global variable whose value is different depending on how the Python file is executed, and if the Python file is executed directly then its variable value is __main__, If it is executed indirectly as a module, then its variable value is the module name, and with this we can control the code in a python file executes directly when executed or does not execute Five, module search orderwhen searching for modules, Python looks for the loaded modules in memory and then finds them in the built-in modules. Finally from the path contained in the Sys.path in order to find, here we need to note that we define the module name is not the same as the built-in module names, otherwise the names of the built-in module can not be used vi. python compiled filesThe Python interpreter executes the first loaded module once in the execution of Python. pyc files, the next time you will be loading this. pyc file instead of the module file, the advantage is that the module will load faster, but the default will not be generated for other Python files. pyc file , we can use python-m xxx.py to get Python to this Python file and generate a. pyc file when you run a python file. Seven, the packagethe package in Python is actually a folder that contains the __init__.py file, and when we import a Python package, we actually import the file. Absolute Import: The specific submodule path is specified in from, such as from glance.cmd Import managerelative import: is to use points to represent the path before the submodule, a few layers outside the sub-module will require a few points, such as from. CMD import manage. Relative imports can be used only within the same package and only if the submodule is executed by another file import.
Python module Import