Five, the organizational structure of Python

Only if the file is added to the folder, this folder is regarded as a package, and the running package will execute the 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

import from a module

from module_b import n1

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 *

Introduce multiple variables or modules

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


​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

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

2. After importing a package or module, as well as a variable, the 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 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 for use by other py files


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

Use the sys library in the 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


Under the a package module, query the variable content

Use the module of the b package as the executable entry file, and call the 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('package:'+(__package__ or 'The current module does not belong to any package'))
print('doc:'+(__doc__ or 'The current module has no documentation comments'))


call module variable content
doc: package_b

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

Executable file, current module variable content
package:The current module does not belong to any package
doc:The current module has no documentation comments

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 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 file, and the modules under the package can be used

Under the 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