Guide package knowledge flask project

A: What package

Appreciated: an __init__.py file is called the package, the package and the package inside the file, in general, will not be performed manually, is to be referenced, and then executed by the unified function entry.

test1.py

def add1():
    print("add1")

test2.py test3.py test4.py and test1 function, the function names are add2, add3, add4, __ ini__.py is empty

A: test2.py used in test1 and ADD2 () function

from test2 import add2

def add1():
    print("add1")
    add2()


if __name__ == '__main__':
    add1()

# 结果
add1
add2

II: Use test3.py in test1 and ADD3 () function

from test3 import add3

def add1():
    print("add1")
    add3()


if __name__ == '__main__':
    add1()

# 报错
ModuleNotFoundError: No module named 'test3'

The reason is that the process from test3 import add3 execution is such that, under the current directory to test1 where to find test3, after finding into the use of inside add3 () function, but could not find, because only three of the directory where test1 file, tes1.py, test2.py, demo folder.

from demo.test3 import add3

def add1():
    print("add1")
    add3()


if __name__ == '__main__':
    add1()

#  结果
add1
add3

 

The reasons for the success are: first find a demo in the current directory, and then find a test3 in the demo directory, and finally found add3, the starting point of the lead pack, is the directory where the execution lead pack of this file, be looking to find not on the error.

from demo import test3

def add1():
    print("add1")
    test3.add3()


if __name__ == '__main__':
    add1()

# 结果
add1
add3

 

This form can also go to the first demo, demo found by test3, finally found add3 () 

3: Use in test3 test4 inside ADD4 () function

from test4 Import add4 # However test4 add4 and wavy line are red 

DEF ADD3 ():
     Print ( " ADD3 " ) 
    add4 () 

IF  the __name__ == ' __main__ ' : 
    ADD3 () 

# result 
ADD3 
add4

 

While succeed, but there are still some problems, when performed manually test3.py time, no problem, it can output, but also to be someone else when test3 package when used, will be a problem, do not change test3.py

Let test1 test3 used inside add3 use

from demo import test3

def add1():
    print("add1")
    test3.add3()


if __name__ == '__main__':
    add1()

# 报错
Traceback (most recent call last):
  File "C:/Users/Administrator/PycharmProjects/untitled/test1.py", line 1, in <module>
    from demo import test3
  File "C:\Users\Administrator\PycharmProjects\untitled\demo\test3.py", line 1, in <module>
    from test4 import add4
ModuleNotFoundError: No module named 'test4'

 

Because at this time of the entry guide package is the folder where the file test1, first found by test3 demo, and then perform test3 inside the first line of code, from test4 import add4 here on the problem, this time from a file or folder where test1 find test4, so it is wrong.

If you want to execute properly, you can add at this time is necessary to amend test3.py

from .test4 import add4
def add3():
    print("add3")
    add4()

if __name__ == '__main__':
    add3()

 

Then execute test1.py

from demo import test3

def add1():
    print("add1")
    test3.add3()


if __name__ == '__main__':
    add1()

# 结果

add1
add3
add4

 

The reasons for the success are: After entering test3.py, the first sentence of the code execution time, from .test4 import add4, this plays a big role, represents the current file folder where the first reading to find test3... where py file folder, demo, demo then find test4 from inside, sure to be successful.

IV: guide packet __all__

__all__List is a string, is used to define the module for from XXX import *an excuse to the external export symbol, i.e., be exposed, but only to import *act on the from XXX import XXXineffective.

For example, to increase the inside __init__.py __all__ = [test3.py], then perform test1.py

from demo import *

def add1():
    print("add1")
    test3.add3()


if __name__ == '__main__':
    add1()

# 结果
add1
add3
add4
from demo import *

def add1():
    print("add1")
    test4.add4()


if __name__ == '__main__':
    add1()

# 结果
add1
Traceback (most recent call last):
  File "C:/Users/Administrator/PycharmProjects/untitled/test1.py", line 9, in <module>
    add1()
  File "C:/Users/Administrator/PycharmProjects/untitled/test1.py", line 5, in add1
    test4.add4()
NameError: name 'test4' is not defined

 

__all__ is to protect the package, avoid exposure to all things, a list of which only specified method names, variable names are allowed to be used

__Init__.py to modify __all __ = [ "test3", "test4"]

Continue test1.py

from demo import *

def add1():
    print("add1")
    test4.add4()


if __name__ == '__main__':
    add1()

#结果
add1
add4

 

 

 

 

# ALL

Guess you like

Origin www.cnblogs.com/meloncodezhang/p/12633226.html
Recommended