Original link: https://chegva.com/5426.html
◎Knowledge points
-
Use modules from the current project
-
The same property exists in different imported modules
-
Execution process of import statement
-
Path where the interpreter searches for modules
◎Script practice
▽ Use the modules in the current project
""" If you want to use the modules in the current project, you must import them through the import statement. There are three import methods: 1. Direct import 2. Absolute import 3. Relative import """ """ 1. Direct import Directly import the module name in the current directory 2. Absolute import Import the absolute path of the module name 3. Relative import Import the relative path of the module name. Among them, one . represents the current directory, and two .. represent the parent directory of the current directory. When a module is run directly, the module becomes the main module. The main module is located at the top level and cannot form a relative relationship with other modules in the same directory. Therefore, when running a module directly, the module cannot use relative import """
[zkqw]
▽ The same properties exist in different imported modules
""" When the same attribute exists in different imported modules, for example: when there is a variable v with the same name in the two imported modules moda and modb, 1. Wrong import method: from modb import v from moda import v The later imported properties will overwrite the previously imported properties. 2. Two correct import methods: (1) Give the imported property an alias from moda import v as va from modb import v as vb (2) Import the entire module import moda import modb """
▽ Execution process of import statement
""" When importing a module using the import statement, the interpreter will look up whether the module has already been imported according to the modules attribute of the sys module.""" import pprint as p, sys p.pprint(sys.modules) """ 1. If the module has been imported, the interpreter does nothing 2. If the module is not imported (1) The interpreter searches for the module according to a certain path (2) Compiles the searched module into a pyc bytecode file (can select) (3) Execute the compiled bytecode file to run the module """
▽ The path where the interpreter searches for modules
""" When importing a module using the import statement, if the module has not been imported, first, the interpreter will search for the module according to a certain path 1. Example of the interpreter searching for a module """ # import my_module # my_module.f() # f is called """ 2. The path for the interpreter to search for the module The path for the interpreter to search for the module is stored in the variable path of the module sys Execute from the interactive command line: >>> import sys >>> sys.path ['', '/Library/Frameworks/Python.framework/Versions/3.6/lib/python36.zip', '/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6', '/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/lib-dynLoad', '/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages'] The search path mainly consists of three parts: (1) Current directory (2) Standard library directory (3) Third-party library installation directory""" """ 3. Modify the path for the interpreter to search for modules The first way: directly modify sys.path According to the above print results: sys.path is a list. You can directly modify sys.path to modify the search path when the code is running, but the modification will fail after the code is running""" import sys sys.path.insert(0, '/Users/zhangrongchao/Downloads') """ Copy my_module.py to the directory /Users/zhangrongchao/Downloads and make the following modifications: def f(): print('f was called (Downloads directory)') """ import my_module my_module.f() # f is called (Downloads directory) """ Execute from the interactive command line: >>> import sys >>> sys.path Search path unchanged""" """ The second way: set the environment variable PYTHONPATH to modify sys.path The path corresponding to the environment variable PYTHONPATH will be automatically added to sys.path. The modified search path will still be valid after the code is run."""
◎Script address: https://github.com/anzhihe/learning/blob/master/python/practise/learn-python/python_advanced/module2.py
[/zkqw]
This article is reprinted from: https://chegva.com/5426.html
This site is for inclusion only, and the copyright belongs to the original author.