Python from entry to practice: the use of packages

As the number of modules increases, it is extremely unreasonable to put all modules together without distinction, so Python provides us with a way to organize modules together, that is, to create a package. A package is a folder containing _init_.py files, in which submodules or subpackages can be organized , for example

Let's give an example directly:

This is the structure of the file: there are two packages in the saaa package, foo and foo2, in which m1.py and m2 are the same

 Let's understand one thing first: what file to execute, the execution order of the file is the same, and the addressing method is the same. For example, if I want to execute test.py, I will look at its execution order (environment variables are based on the execution file Yes, all imported modules or the sys.path referenced by other subsequent files refer to the sys.path of the execution file )

The execution order of the files in foo and foo2 is also in accordance with this, let's look at the two definitions of the two files:

 

 

 

 

1. The import statement related to the package is also divided into import and from ... import ..., but no matter which one, no matter where it is, one principle must be followed when importing: all imports with dots , the left side of the dot must be a package, otherwise it is illegal. It can have a series of points, such as import top-level package. Subpackage. Submodule, but must follow this principle. But after importing, there is no such restriction when using. The left side of the dot can be package, module, function, class (they can all use dots to call their own attributes) .
 
2. There will be no conflict if there are modules with the same name under package A and package B. For example, Aa and Ba come from two namespaces.
 
3. When importing a file, the name in the generated namespace comes from the file, the import package, and the generated namespace The name also comes from the file, that is, __init__.py under the package. The essence of importing the package is to import the file

Notice:

You can also use the __all__ variable provided by the module. The value of this variable is a list that stores the names of some members (variables, functions or classes) in the current module. By setting the __all__ variable in the module file, when other files import the module in the form of "from module name import *", only the members specified in the __all__ list can be used in the file . (__all__ will only restrict *, if it is imported in other forms, it will not be restricted) __all__ is also a convention for the public interface of the module. Compared with underscore, __all__ provides a "white list" for exposing the interface.

# mytest.py
__all__ = ['a','b']  #这里不同

def a():
    print('aaaaa')
def b():
    print('bbbbb') 
def c():                  # 这里不同
    print('ccccc')
    
# mytest2.py
from mytest import *
a()
b()
c()

 

Traceback (most recent call last):
  File "/home/mytest2.py", line 6, in <module>
    c()
NameError: name 'c' is not defined
aaaaa
bbbbb

Guess you like

Origin blog.csdn.net/weixin_43507744/article/details/126592190