Abstract: The relative path and absolute path reference method are explained in detail.
The following two errors occurred during a run:
ModuleNotFoundError: No module named '__main__. Src_test1 '; 3: ImportError: CHS relative import with no known parent packageCopy the code
So based on these two errors explore python3 module reference problem, the following parsing, please read patiently.
Ok, let’s construct the first error:
The test code structure is as follows:
|--- test_main.py
|--- src
|--- __init__.py
|--- src_test1.py
|--- src_test2.py
Copy the code
Src_test2. Py code
Python code
class Test2(object):
def foo(self):
print('I am foo')
Copy the code
Src_test1.py code, referring to the Test2 module
from .src_test2 import Test2
def fun1():
t2 = Test2()
t2.foo()
if __name__ == "__main__":
fun1()
Copy the code
No module named ‘__main__. Src_test1 ‘; ‘__main__’ is not a package ‘
Cause of the problem:
Src_test2 / src_test2 / src_test2 / src_test2 / src_test2 / src_test2 / src_test2 / src_test2
From PEP 328, we find an introduction to the Relative imports (relative references)
The name of the main module is’ main ‘, and the dot (.) in the import of this module is added. Replace it with ‘__main__’, then.src_test2 becomes __main__. Src_test2, so of course the module cannot be found.
Solutions:
Therefore, the recommended approach is to create the reference module test_main.py in the SRC directory at the same level and reference the src_test1 module as follows:
from src.src_test1 import fun1
if __name__ == "__main__":
fun1()
Copy the code
Test_src code:
from src_test1 import fun1
if __name__ == "__main__":
fun1()
Copy the code
ImportError: CHS relative import with no known parent package
Cause of the problem:
Src_test1: name = src.src_test1: name = src.src_test1: name = src.src_test1: name = src.src_test1: name = src.src_test1: name = src.src_test1 In this case, its absolute path is src_test1, and then reference the relative path lookup test2 again. In the same step, it needs to find the parent node first, but now it is the root node, and there is no parent node, so the error “no known parent package” is reported.
Solutions:
In order to avoid parent node conflicts, remove relative references from introductions in test1
from .src_test2 import Test2 --> from src_test2 import Test2
Copy the code
Going further:
How does the compiler find this module using relative and absolute paths?
If the import file is not found in the lib library, then the sys.path is searched. If the import file is not found, then the sys.path is searched.
Src_test2 can be found in both the current directory and the directory in sys.path.
extra:
Either way, we debug the code in a single file, but in this case the root directory is wrong and the import mode needs to be changed, which is cumbersome, so we recommend using sys.path.append() instead
import sys,os
sys.path.append(os.getcwd())
from src.src_test2 import Test2
Copy the code
Path, and then refer to the absolute path. This method can be called regardless of the first or second implementations above, or test1 can be compiled separately without changing the import path, which is relatively safe. But the downside is that if you change a package name, you need to change all references, which is a lot of work, so adapt to local conditions.
In summary, you have explained the relative path and absolute path reference methods in detail, and now you should have a clear understanding of the import problem
Note: this article is based on Python3.7
Click to follow, the first time to learn about Huawei cloud fresh technology ~