Let me preface this by saying that I understand that imports are a frequently touched-upon/complained-about topic in the Python community. I only wish that a simple import system is developed but alas. Being as that is, I have referred a lot of places for potential solves already; please check in the below paras the links to other SO question threads which I've already been through.
Here's the directory structure for my project:
I wish to import file1.py from folder1 of the main_folder into my code in file2.py.
Hence the importing code in file2.py is:
from main_folder.folder1.file1 import some_func
I try to run file2.py normally from the application directory (python main_folder/folder2/file2.py
)
I have also tried the following possible solves in the same conditions (all to no effect):
from application.main_folder.folder1.file1 import some_func # after adding init to application folder
and...
from folder1.file import some_func
and...
from .folder1.file1 import some_func
and...
from ..folder1.file1 import some_func
I have tried the answers suggested at this other question as well. The second-most popular answer seems to be the most pythonic way to do it and I am indeed trying that very approach above. However, that answer does not seem to work for a lot of people (and I) who have indicated this in the comments and have suggested opening another question regarding the same.
If this approach does not work, then what sys.path.append()
command should I add at the top of file2.py to be able to from folder1.file1 import some_func
? I understand that this sys.path.append method isn't the most pythonic way to do it but unfortunately we seemingly have no better solve until the guys developing Python set something in motion.