I would like to import a file that also imports another file.
I currently have the following directory structure:
.
├── main.py
└── foo
├── file1.py
├── file2.py
└── file3.py
With the following code:
# main.py
from foo.file1 import func1
func1()
# foo/file1.py
from file2 import func2
from file3 import func3
def func1():
# Do stuff
func2()
func3()
if __name__ == "__main__":
# Do some other stuff
func1()
# foo/file2.py
from file3 import func3
def func2():
# Do stuff
func3()
# foo/file3.py
def func3():
# Do stuff
If I run main.py
, I get ModuleNotFoundError: No module named 'file2'
.
I could replace the line from file2 import func2
in foo/file1.py
with from foo.file2 import func2
and do the same for the file3 import but then I could not run foo/file1.py
on its own.
What would be the recommended way to fix this?