Five, the organizational structure of Python

Only if the _init_.py file is added to the folder, this folder is regarded as a package, and the running package will execute the _init_.py file first.

1. Import the module

​import, you need to refer to variables of other modules, you can use import

Import functions and variables

from module_b.n1 import a
print(a)

import from a module

from module_b import n1
print(n1.a)

import all variables

from module_b.n1 import * - import all variables.

Note: If you import some modules, you can add __all__ = ['a','b'] to the n1 file, and only two variables a and b are imported at this time.

# import all variables
from module_b.n1 import *
print(a,b,c)

Introduce multiple variables or modules

# When introducing multiple variables or modules,\newline
from module_b.n1 import a,b,\
c
# When introducing multiple variables or modules, () wraps the line
from module_b.n1 import (a,b,
c)

Precautions

​import to avoid circular import, or indirect circular import of modules, such as a module import b module, b module import a module

import import, when a imports module b, executing module a will also execute all the code of module b

2. Introduction to __init__.py

1. A package, equivalent to a folder, becomes a package and needs to add a special file, the __init__.py file, the module name is the package name

2. After importing a package or module, as well as a variable, the __init__.py file will be automatically executed when the imported py file is executed.

For example: import the b package into the c package, and execute the file of the c package, the __init__.py file in b will be automatically executed first

3. When multiple py files in a package need to reference multiple libraries, the libraries that need to be imported can be added to __init__.py for use by other py files

E.g:

In the __init__.py file in the secondary directory packag_b package, import 3 libraries

Use the sys library in the m1.py file in the first-level directory packag_a package

3. Built-in variables in modules

1. Query the built-in variables of the module through the dir method, for example: print(dir())

['__annotations__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__']

When importing a package, use dir() and add parameters to view all variables in the package, such as dir(sys)

2. When module a calls module b, the content can be queried through built-in variables as follows:

__name__, query calling module name

__package__, query the package of the calling module

__doc__, query calling module comments

__file__, query the py file path of the calling module

E.g:

Under the a package n1.py module, query the variable content

Use the m3.py module of the b package as the executable entry file, and call the n1.py module of the a package

print('call module variable content')
import package_b.n1
print('\n'+'dividing line'.center(50,'#')+'\n')
print('Executable file, current module variable content')
print('name:'+__name__)
print('package:'+(__package__ or 'The current module does not belong to any package'))
print('doc:'+(__doc__ or 'The current module has no documentation comments'))
print('file:'+__file__)

result

call module variable content
package_b.n1
name:package_b.n1
package:package_b
doc: package_b
file:/Users/zhangchen/Downloads/Py_study/package_a/package_b/n1.py

#######################dividing line########################

Executable file, current module variable content
name:__main__
package:The current module does not belong to any package
doc:The current module has no documentation comments
file:/Users/zhangchen/Downloads/Py_study/package_a/m3.py

4. Matters needing attention

By adding an if __name__ == '__main__': statement to the py file, the current file is an executable file or a module. When the a module is placed in this statement, the a module is called in the b module, and when the b module is executed , the code under this statement will not be executed

Use python3 -m module_b.n1.py to use executable files as modules

The concept of a top-level package - is the top level of an executable's sibling package

Use "." to import variables with relative paths, such as from .m3 import m

Note: The entry file cannot be imported using a relative path. If you want to execute the module, you need to use the command python3 -m

9. When multiple modules need to reference the same built-in library, they can be imported directly in the __init__.py file, and the modules under the package can be used

Under the __init__.py file, import the built-in library that needs to be imported.

In other module references, you only need to import + package name, you can adjust the variables in the built-in library

Tags: Python Deep Learning Flask Ubuntu programming language

Posted by NME on Fri, 21 Oct 2022 00:55:55 +1030