I am working in python3 for a year now and I still don't get the import system....
I always put the empty init.py file on each folder.
But however if my project start to have nested directories (and it append a lot and often) I have to run a war to be able to import my own module (who python thinks he is ????)... It is driving me crazy.
So for now the 'best' trick I have found, but clearly I am not satisfied with it is to put ligne like this on top of my file:
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(file))))
I do not like it, first because when the directory is nested on several level the line is extraordinary long, then because my linter is always yelling at me because my import are not on top of my files.
So I was wondering, what am I missing ? Is there an information I missed ? What's your technique ?
Thank you !
Can't import my own modules in Python
Someone suggested it's similar to this answer but it is not.
First I do not want to use relative import.
Then the 'sys.path.append("..")' is not clean in my opinion, and my imports are still not on top of my files.
Basically let's say I have a structure like:
folder_a
----file_1
----file2
----folder_b
--------file_3
--------folder_c
-----------file_4
I am instanciating a class from file_1 in file_3,I have to do this:
sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(file)))))
from folder_a.file_1 import class_1
doing this obviously won't work:
sys.path.append("..")
from folder_a.file_1 import class_1
This is so not logic for me