import sys
sys.path.append(".")
from . import module
Package structure:
TestPackage
|-> init.py
|-> module.py
from . import module
ImportError: attempted relative import with no known parent package
Direct command line execution: python PATH_TO_INIT.PY gives the above error Anyone know why im getting this error?
import sys
sys.path.append(".")
from . import module
Package structure:
TestPackage
|-> init.py
|-> module.py
from . import module
ImportError: attempted relative import with no known parent package
Direct command line execution: python PATH_TO_INIT.PY gives the above error Anyone know why im getting this error?
Share Improve this question asked 2 days ago Pacific EastPacific East 111 bronze badge New contributor Pacific East is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.1 Answer
Reset to default 0Directly executing a package's __init__.py
isn't expected to work. Whatever goal you were hoping to achieve by doing that, you should find a different way to go about it.
If you try to do it anyway, it won't be considered the package's __init__.py
when you do that. It'll just be considered some random .py
file, not part of a package at all, and you can't perform relative imports from some random .py
file that isn't part of a package.
Also, your import path will be messed up, and sys.path.append(".")
doesn't fix it.