19. Modules and Packages

What is a package: Simply put, a folder with an _init_.py file is a package. When we call this package, the _init_ file in the package is executed first. The other py files in the package are modules

1. When we need to call a module directly, but we are not sure if we can call it directly, we can do this

import sys

print(sys.path)

Look at the first item in this list. The folder directory of the py file you are currently in is the first item, and the modules and packages in this folder directory are what you can call.

2. When the module and the module call each other, it will only be called once, and there will be no repeated calls, because once the module is called successfully, it will be added to the sys.modules dictionary, that is to say, when you call the module, first The python interpreter will automatically find out if there is any in the sys.modules dictionary, and some will be used directly, and will not be called again, so the module will only be called once.

3. The problem of cross-package calling is that I have been tangled for a long time on how to call the module with the bag. This fucking can't be done at all. I have been entangled in this place for a lot of time. Now I finally get it, and now I write it out to remind myself, for the following reasons:

This is a path diagram. Here I want to call b.py from c.py. ! ! ! ! !

But I just want to do it. .

Whenever we encounter this kind of problem, we have to immediately think of two questions: 1. What is the first folder of the sys.path of the py file you are in? 2. When executing a package, you must first execute the _ under the package. init_file. Sao Nian, do you remember these two things? ? ? ?

Go oh, we have been chatting for a long time and still haven't said what to do. . . . I'll tell you now! ! ! ! At this time, we carefully observed and found that c.py and b.py are in mode3 and mode2 respectively, and these two packages are in mode1. At this time, we can only, yes, only , only in py at the same level as mode1 In the file, c.py executes b.py by executing a series of relationships. As for the specific operation, it is very simple, just write some calling modules in the _init_ file in each package.

4. The absolute path of the module call, the way of writing is to clearly write the path of the calling module in the ini file of each package, and then execute it in the py file in the same directory as the package, but there is a problem with this, such as the one in the figure above , the path of mode1 changes, the path in the init file of all packages must be changed, so there is a relative path call.

5. The call of the relative path is the same as the writing of the absolute path, which is to replace the directory of the current py file with . The parent directory is replaced with .. That's it, and the cross-directory call problem mentioned above can only be implemented outside. , the reason is because a py file cannot be executed both as a module and as a script.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325069852&siteId=291194637