└── Project
├── dir1
│ ├── __init__.py
│ └── module1.py
│ └── module1_2.py
├── dir2
│
│ └── module2.py
├── file_a.py
└── main.py
1. In main.py, you want to use from . import file_ Import error: attempted relative import with no known parent package
The error message means that an attempt was made to import using a relative path, but a known parent package could not be found. Generally speaking, this error occurs when you try to use relative path import in a running. Py file. The reason for the error is that the relative path import of Python actually needs to be implemented with the help of the parent package path of the current file, that is, by judging the path of a. Py file__ name__ And__ package__ Property to get information about the parent package. In a running. Py application file, there are:
if __name__ == '__main__':
This introduces the main entry into the file. At this time, the file __ name__’s attribute is’ ‘__ main__’ And __ package__’s attribute is none. If the relative path is used in such a file, the interpreter cannot find any information about the parent package, so an error is reported.
Python seems to have a setting. The directory of the current executable file will not be treated as a package.
To solve the problem, you can add the following code to the file where the import code is located
print('__file__={0:<35} | __name__={1:<20} | __package__={2:<20}'.format(__file__,__name__,str(__package__)))
The running result shows: __name__ = __main__ and __package__ = None, the python interpreter does not have any information about the package to which the module belongs, so it throws an exception that the parent package cannot be found.
solution:
Import directly from the module
from file_a import xxx
2. When I want to use from .. import file_a in module1.py, an error message is reported: attempted relative import beyond top-level package
The reason is the same as the above situation, that is, the current project directory is not a package, that is, the cheating setting of Python, and the current directory of the project entry will not be regarded as a package.
solution:
The content of file_a.py to be imported in module1.py:
from file_a import xxx
To import the content of module1_2.py in module1.py, you can use relative import:
from. import module1_2 as module12
Read More:
- Python 3 uses the relative path import module
- Python Import Error: SystemError: Parent module ‘‘ not loaded, cannot perform relative import
- [Solved] Python Project Import Module Error: ModuleNotFoundError
- Python server run code ModuleNotFoundError Error [How to Solve]
- An introduction to sys modules in Python and how packages are imported and used
- [Solved] SystemError: Parent module ” not loaded, cannot perform relative import
- cffi Security Report “c / _cffi_backend.oc / _cffi_backend.c: 15:17: error: ffi.h: No such file or directory” Problem
- [Solved] RuntimeError (note: full exception trace is shown but execution is paused at: <module>)
- How to Solve PyInstaller Package Error: ModuleNotFoundError: No module named ‘xxxx‘
- The solution of no such file or directory and cannot load native module running error of python3 pyinstaller after packaging
- [How to Solve] ImportError: No module named typing
- Python FileNotFoundError: [Errno 2] No such file or directory: ‘objects/epsilon.pkl
- [Solved] Pytest Error: E ModuleNotFoundError: No module named ‘common
- Python recursively traverses all files in the directory to find the specified file
- Full explanation of SYS module of Python
- [zipfile] Python packages files as zip packages & decompresses them
- [Solved] Pycharm from xx import xx Error: Unresolved reference
- [Solved] Pycham Error: non zero exit code (2)
- The automatic token of Python interface is passed into the header
- Invalid python sd, Fatal Python error: init_fs_encoding: failed to get the Python cod [How to Solve]