I want to import another Python file in the same folder, for example a.py
with contents:
from ..b import b
...
I always meet ImportError: attempted relative import with no known parent package.
When I try to import it with m = importlib.import_module('a', '.')
, I also try importlib.util
stuff, and it doesn’t work either.
Replacing with from . import a as m
at the same place works, but I want to import it according to the input string, so it leads to this problem.
The only way I found is:
md = {'__name__': globals()['__name__']}
exec('from . import a as m', md)
a = md['m']
uUimport statement and calling importlib
function?
2