I found similar discussion here.
But my problem is it works running python code in normal mode.
When I run in debugging as python -m pdb nmt.py, I have ImportError: attempted relative import with no known parent package
. My python version is Python 3.7.13.
Error is coming from the following line.
from . import inference
How to make it work in debugging?
Advertisement
Answer
Explanations
The error message is actually pretty explicit:
Relatives import (using from . import module
, or from .module import something
) Only work if the module (ie the file) you are editing, and the module you are importing are in the same python package.
That means you need a __init__.py
file in that directory (for it to be considered a package).
I recommend you read the documentation on modules if you want to understand the import system better
Quick fixes
- either add a
__init__.py
to make your directory into a package - or change your relative import to a normal import (ie. change
from . import inference
toimport inference
.) — you may need to add the directory containing your files tosys.path
(documentation)