I have a bunch of data processing scripts in the root directory of my Python application that I want to clean up by putting into their own directory, but I can't get it to work properly.
This is my file structure:
.
+-- Root
|
|-- main_script.py
|
+-- utils
| |
| |-- utilsA.py
| `- utilsB.py
|
+-- processing_scripts
|
`- proccess.py
Within process.py
there is the line from utils import utilsA as u
, then within utilsA.py
there is the line from utils.utilsB import util_function
.
When process.py
is located in the root directory, this works completely fine, however when I move process.py
to the processing_scripts
directory and try to run it from there, it no longer works. It just says ModuleNotFoundError: No module named 'utils'
.
I did a search and found a number of similar questions on here, the answers to which mostly suggest to use some variant on sys.path.append('../')
. This solution works if utilsA.py
has no reference to utilsB.py
, and I change the line in process.py
to from Root.utils import utilsA as u
, but as soon as utilsA.py
tries to reference utilsB.py
, it gives the same error: ModuleNotFoundError: No module named 'utils'
.
Is there a way of tricking process.py
into thinking it is running from the root directory, so that it can then reference things, which reference other things, without breaking? I can get it to work if I go into utilsA.py
and add the sys.path.append('../')
line, as well as changing the reference to utils.utilsB
to Root.utils.utilsB
, but I would rather not have to go through and change every single file like that, just to run some processing scripts - and that solution feels incredibly hacky..