1

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..

guskenny83
  • 1,321
  • 14
  • 27
  • 1
    Do note that Python imports _do not_ navigate directories. Imports are only resolved by searching the Python path. If you want `from utils import ...` to work, then `utils` must be a package on the Python path. That is usually accomplished by either installing your project as a distribution project or by ensuring that the directory _containing_ `utils` is your CWD. – Brian61354270 Mar 15 '23 at 03:32
  • 1
    Potentially helpful/related: [Relative imports for the billionth time](https://stackoverflow.com/q/14132789/11082165) – Brian61354270 Mar 15 '23 at 03:33
  • "and try to run it from there, it no longer works." so, since it seems like you haven't packaged/installed this project, then you are relying on the wording directory being added to the path, hence, whether your import works or not depends on where you run it from. – juanpa.arrivillaga Mar 15 '23 at 03:54
  • Thanks for the advice everyone. I did a bit more reading and it seems that my understanding of imports/modules/packages in Python was pretty off. It is not worth turning what I am working on into a full package, so I changed all the import lines that pertain to my files, to relative imports with `.` and `..`, and added `sys.path.append('../')` to my `process.py` script - which seems to work. Thanks again! – guskenny83 Mar 15 '23 at 05:15

0 Answers0