Import of the basic package of python and the introduction of succes1EN__

  • 2020-06-23 00:59:23
  • OfStack

Call sibling directories:


 �  src 
| �  mod.py 
| �  test.py

If you import the module mod into the program ES4en.py, use it directly


import mod
 or 
from mod import *

Call the module under the subdirectory:


 �  src 
| �  mod.py 
| �  lib 
| | �  mod2.py 
| �  test.py

from lib.mod2 import *

 or 

import lib.mod2

Call the file in the parent directory

The directory structure is as follows:


 �  src 
| �  mod.py 
| �  lib 
| | �  mod2.py 
| �  sub 
| | �  test2.py
import sys
sys.path.append('C:\\test\\A\\C')
import mod
import lib.mod2

__init__.py

Per each package of the python module, there is a file with each ___ 22en__.py (this defines the attributes and methods of the package), followed by 1 module file and subdirectories, which would be the subpackage of that package if there were also ___ 24en__. When you import a package as a module (for example, import dom from xml), you actually import its ___ 28en__.py.

Per package is the directory with the special papers per ___ 32EN__.py. The file of init__.py defines the properties and methods of the package. It can actually define nothing; It can be just an empty file, but it must exist. If ___ 36EN__.py does not exist, the directory is simply a directory, not a package, and it cannot be imported or contain other modules and nested packages.

___ There is also an important variable in py, called ___ 42EN__.

If at this point the directory is as follows


 �  src 
| �  mod.py 
| �  lib 
| | �  mod2.py 
| | �  mod3.py 
| | �  sub 
| | �  | | �  mod3.py

We sometimes do a "all import" trick, which goes something like this:


from lib import *

import then imports the submodules and subpackages registered in the list of ___, with the package ___ 52en__.py file, and with the subpackages. Such as:


# file __init__.py
__all__ = ["mod2", "mod3", "sub"]

Conclusion:

For the rest of the path to download the py file, to import, add to sys.path before importing.

___ The role of py.

The identity of package cannot be deleted Define each ___ in package and use it to blur the import Write the Python code (it is not recommended to write the python module in ___ 72EN__, you can create another module in the package with each ___, and keep it as simple as possible.)

conclusion


Related articles: