ImportError: attempted relative import with no known parent package 🙁
Question:
I’m attempting to import a script from my Items file but I keeps on getting an error
from .Items.Quest1_items import *
gives
from .Items.Quest1_items import *
# ImportError: attempted relative import with no known parent package
# Process finished with exit code 1
Here my project tree, I’m running the script from the main.py file
Quest1/
|
|- main.py
|
|- Items/
| |- __init__.py
| |- Quest1_items.py
Answers:
Remove the dot from the beginning. Relative paths with respect to main.py
are found automatically.
from Items.Quest1_items import *
You can only perform relative import (ie., starting with a dot), inside a package that you import. For instance, imagine the situation:
project/
├ main.py
├ mylib/
├ __init__.py
│ ├ module1.py
│ └ module2.py
in main.py
, you would have import mylib
or from mylib import *
,
but inside module1.py
, you could have from . import module2
, because here the .
stands for mylib
(which is a python package, because you imported it within main.py
).
So, the solution is simply remove the dot, it’s not useful in your situation.
To put it simply: if you use relative import, you can run the file you want to run with ‘python -m your_module_path’ on the two layers above the outermost file used by your code.
Like the following, if you want to run run.py
, you need to go to two layers above it, then run python -m dir1.dir2.run
(without .py
).
.../dir1/dir2/
-test
-test1.py
from .test2 import *
-test2.py
-run.py
from .test.test1 import *
I’m attempting to import a script from my Items file but I keeps on getting an error
from .Items.Quest1_items import *
gives
from .Items.Quest1_items import *
# ImportError: attempted relative import with no known parent package
# Process finished with exit code 1
Here my project tree, I’m running the script from the main.py file
Quest1/
|
|- main.py
|
|- Items/
| |- __init__.py
| |- Quest1_items.py
Remove the dot from the beginning. Relative paths with respect to main.py
are found automatically.
from Items.Quest1_items import *
You can only perform relative import (ie., starting with a dot), inside a package that you import. For instance, imagine the situation:
project/
├ main.py
├ mylib/
├ __init__.py
│ ├ module1.py
│ └ module2.py
in main.py
, you would have import mylib
or from mylib import *
,
but inside module1.py
, you could have from . import module2
, because here the .
stands for mylib
(which is a python package, because you imported it within main.py
).
So, the solution is simply remove the dot, it’s not useful in your situation.
To put it simply: if you use relative import, you can run the file you want to run with ‘python -m your_module_path’ on the two layers above the outermost file used by your code.
Like the following, if you want to run run.py
, you need to go to two layers above it, then run python -m dir1.dir2.run
(without .py
).
.../dir1/dir2/
-test
-test1.py
from .test2 import *
-test2.py
-run.py
from .test.test1 import *