I've read PEP 328 and similar questions like this, but they still didn't answer my questions about python 3 import. My question is:
why does a single dot .
relative import ever produce an error like the following?
code: (__init__.py
exists in the same directory)
from . import module_x
error:
ImportError: attempted relative import with no known parent package
Yes, there are workarounds to get around of this error for example by running python -m
from the parent directory, but why?
Shouldn't .
always refer to the same directory of the current module and the example always works as long as the module_x
exists in the same directory?
Particularly PyCharm
tries to run unittest
test cases from the directory where the test file is, and would always fail the .
import. Some may argue it's PyCharm
's problem, but I think a simple import like this should really always work regardless where the interpreter runs.