My current project has the following structure:
src:
file1.py
file2.py
There are some functions in file2 which I would like to import into file1 and use. So the file 1 has the following lines:
from file2 import func1, func2
When run the terminal in the src directory and type:
from file1 import *
everything works well. However, when go outside the directory src, and type in the python terminal
from src.file1 import *
I get the following error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/bhavincvts/Desktop/greenleap/solarAFD1/src/file1.py", line 3, in <module>
from file2 import func1, func2
ModuleNotFoundError: No module named 'file2'
After that, I tried changing the import statement to,
from .file2 import func1, func2
it then works well from outside the src folder. But when running the terminal inside the src folder, it shows the following error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/bhavincvts/Desktop/greenleap/solarAFD1/src/file1.py", line 3, in <module>
from .file2 import func1, func2
ImportError: attempted relative import with no known parent package
Is there anyway to fix this?