python notes: packages and modules

basic concepts

Module: a set of codes for related functions, which is a single .py file. Package
: a folder composed of several modules or sub-packages. There is generally an __init__.py file in the directory [not required after python3.3]
Library: a collection of codes that complete a certain function, which can be embodied as a package or module
framework: a support with certain constraints designed to solve a problem structure. Through a framework, you can quickly realize the skeleton of a problem and then fill it in specifically.

The role of packages and modules

1. Write some "wheels" and tool codes for use by other modules; to facilitate maintenance and management.
2. Prevent naming duplication within the same module (each module is a separate namespace)

Classification

【1】Standard package/module <built-in module>: automatically installed when installing python, can be called directly, such as os, sys and other modules.
【2】Three-party packages/modules: Modules developed by some developers for use by others. Additional downloads are required to use, such as torch, tensorflow, etc.
【3】Customized package/module: A module written by yourself will become a third-party package/module if released for others to use.

How to create

  • Create a module: Create a .py file
  • Create a package: Create a folder containing an __init__.py file [not required after python3.3, but recommended to create]; when importing a package, the __init__.py file will be executed first
  • Create a multi-level package: create another package inside the package, which can be infinitely nested

Import method【Key points】

The import statement is used to import other python files (called modules) and use the classes, methods or variables defined in the module to achieve the purpose of code reuse.
Import syntax—regular import

import M ----- Import a single module; if it is a module in a package, you can use dot syntax to locate it [package.module]
import M1, M2 ----- Import multiple modules separated by commas
import M1 as *** ---- Give the module an alias, simplify the access prefix, and enhance the scalability of the code

Note: You need to specify the module name of the resource when using xxx_modelu.run()

import cv2
import numpy as np

cv2.line(img, (lf_x, pos, color, thickness)
result = np.concatenate((result_1,result_2),1)

If you import a package, no modules will be imported by default. There are two solutions:

1. In the __init__.py file, import the required modules again [Importing the package will execute the file]
2. Import it in the form of from ** import **

Import syntax—from statement import
Function: Import part of the module/package
Syntax: from A import B [as C]

  • Rule: Imports can only be made from a large place to a small place, i.e.: Package>Module>Resource
  • Combination: import module from package, import resource from module <cannot jump import>
  • Relationship-oriented: Only modules can be seen in the package, but module resources cannot be seen.

Give a chestnut:

-- P1[1]
------sub_P[子包1]
----------- __init__.py [子包1下的init文件]
----------- sub.py [子包1下的模块]
------ __init__.py [1下的init文件]
------ Tool1.py [1下的模块]
------ Tool2.py [1下的模块]

from P1 import Tool1 as t1  #正确   
from P1.sub_P import sub    #正确
from P1 import sub_P.sub    #错误 面向关系:不能调用导入,包中看不到模块资源

Special case:

from 模块 import *  导入模块中所有资源
fromimport * 

Use it with __ all __ = ["...string name...", "..."] to automatically import things in the list and tell the caller which modules/packages are useful. * represents each string element in the list.
If used alone, it means that all resources that do not start with an underscore will be imported into this location. Use
with caution* because it is impossible to predict what content will be imported at that time, and variable name conflicts are prone to occur.

Module search path
a. Search in sys.modules [cache of all pre-imported modules]
b. Search the built-in module list [pre-installed with Python, Python standard library]
c. Search in sys.path, sys.path is An ordered list with the following search order:

  1. Current directory [the folder where the current executable file is located, has nothing to do with which path you execute the code]: For example, if your program is placed in /home/zhangsan/test.py, when running this program, the sys.path of this program The first path in the list is /home/zhangsan
  2. List of search paths specified in the environment variable PYTHON PATH
  3. The list of file paths contained in the .pth file under a specific path: It is recommended when a large number of external paths need to be added, and it is permanently effective. When starting a Python program, the interpreter will traverse the directory, and when it encounters pth, it will read the contents of this file and add it to sys.path.
  4. Search the Lib library under the python installation path [Third-party package path in the virtual environment]

How to append path

1. Directly modify sys.path: Since sys.path is a list, we can add the corresponding path to the list and use insert and append to append the path. This is added dynamically, and its scope is only the current py file, and is not suitable for the situation where multiple files are introduced at the same time. 2.
Modify the environment variable: PYTHON PATH [the variable value is the path], which is only valid in the shell
3. Add .pth file: Generally stored under lib site_package, the corresponding path is stored in the file

import sys
sys.path.append('./')  #在哪个路径运行脚本/代码,就将该路径添加到sys.path中
sys.path.append('****')

See which paths are currently included

print(sys.path)

View currently loaded modules

print(sys.modules)

Absolute import
1>. The key is to see whether the corresponding module can be found in the search path. Follow the search path of the module to find it [mainly in sys.path]. If it is not found, an error will be reported. In actual projects, the root directory of the project is generally added to sys.path, and subsequent absolute imports are done here. based on the directory.
2>. Pay attention to relationship problems when importing
3>. You can use the two syntaxes import <> or from <> import <>

import a
from a import b

Relative import:
1>. To use relative import, you only need to know the relative position between modules, and use . to represent the relative path:

. : The current directory obtained based on the module name [__ name __]
... : The parent directory obtained based on the module name
... : The grandparent directory obtained based on the module name

2>. Pay attention to the top-level directory problem. The package in the same directory as the executable file is a top-level package. Relative imports cannot span top-level packages, that is, top-level packages cannot access each other [The execution methods of executable files are different, and the top-level directories are also different. For example: python tool/main.py and cd tool & python main.py have different top-level directories]
Insert image description here

Execute main.py, that is, package A and package B in the directory where main.py is located are top-level packages and cannot access each other through relative import, so a.py cannot import b.py in package B. If there is mutual access between top-level packages, the python interpreter will report an error:
ValueError: attempted relative import beyond top-level package
At this time, absolute import can be considered.

3>. Relative import can only be used in the imported module. Modules with relative import statements cannot be run directly. This is because: when a module is run directly, Python considers this module to be the top-level module and there is no hierarchy, so it cannot find other relative paths.

4>. Relative import can only use the syntax from <> import <>, and use . as the leading point.

5>. In the module, you can obtain its own package information through the attribute __ package __, that is, the structure of the package where the module is located, such as XXX, XXX.YYY.ZZZ, etc. The first node is the top-level package. If __ package __ is None indicating that the module is a top-level module. The location of the module is determined by the __ name __ attribute of the module. If it is __ main __, it is a top-level module without a package structure. If it is an ABC structure, then the top-level module is A.

6>. Advantages of relative import for package maintenance: Relative import can avoid package maintenance problems caused by hard coding. For example, if we change the name of a certain layer of packages, then all absolute imports of other modules for its sub-packages will not be available. , but modules using relative import statements will avoid this problem.

Executing code from different path locations mainly affects two places
: 1.sys.path.append('./')
2. Top-level directory during relative import

What operations are performed after the module is imported?

First time import

1. Execute all code in the namespace of the imported module [Whether it is importing the entire module or a function will execute all the code in the module] 2.
Create a module object and set all top-level variables in the module in the form of attributes [can be passed Object.Method access】Bind to the object
3. In the import position, introduce the variable name after import to the namespace [that is, introduce the module object, it can be used]

Second import

Go directly to step three

Whether a module or module resource is imported from a package, all the code in the module will be executed; when the package is imported directly, which modules/module resources are imported by __init__.py in the package, all the code in that module will be executed. Other modules will not be executed; when importing the module again in __init__.py, you need to use the absolute path

Guess you like

Origin blog.csdn.net/qq_44804542/article/details/116462192