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 tried importlib.util
stuffs and it doesnt work either
replacing with from . import a as m
at the same place works, but i want to import it according to 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']
using exec
sounds terrible, but is there another way to achieve this? and how can i use importlib
to do it? what is the difference between the import statement and calling importlib
function?
makpia is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.