I have a folder mypackage
at the location /mypath/mypackage/
, and it contains an __init__.py
file which is empty. mypackage contains two python files: packagefile1
and packagefile2
, and packagefile2
contains import packagefile1
.
When I then call a python script at /myotherpath/script.py
which contains import packagefile2
, it correctly finds packagefile2
, but I get an error :
Traceback (most recent call last):
File "/myotherpath/script.py", line 5, in <module>
from mypackage import packagefile2
File "/mypath/packagefile2.py", line 16, in <module>
import packagefile1
ModuleNotFoundError: No module named 'packagefile1'
However, I don't get this error when I call packagefile2
directly, so it must be because I am not calling the scripts in the mypackage
folder.
What is the appropriate way to solve this?
I have read here that the solution is to change import packagefile1
to import .packagefile1
, but then I read here that relative imports using .
are outdated. So what is the appropriate way to import modules within the same package?
ps: /mypath/
is part of the pythonpath variable (when I command env | grep -i python
in bash, it shows PYTHONPATH=:/mypath/
).