I've been struggling with relative imports on python.
My project structure looks like this:
root_dir
├── main.py
├── Pipfile
├── Pipfile.lock
├── unit_tests
│ ├── __init__.py
│ ├── test_first.py
│ └── test_second.py
└── my_package
├── __init__.py
├── first.py
├── second.py
└── third.py
I want to import a set of functions from a file that is located in my_package
into test_first.py
.
According to the official documentation the syntax should be the following:
from ..my_package import first
When I do that, I get the following exception:
Traceback (most recent call last):
File "/home/user/dev/root_dir/my_package/unit_tests/first.py", line 8, in <module>
from ..my_package import first
ImportError: attempted relative import with no known parent package
I have also tried to import the file by using the following syntax:
from root_dir.my_package import first
When I do that, I get the following exception:
ModuleNotFoundError: No module named 'root_dir'
It is important to mention that I am using pipenv
to handle the virtual environment.
Any idea why this is happening?
Thanks.