pythonmodulecondaimporterrorsys.path

`ModuleNotFoundError` when running Python script with a custom module despite correct `sys.path` setup and conda environment


Problem

I'm working on a Python project with the following structure:

apx-cv/
├── utils/
│   └── custom_object_functions.py
│   └── utils.py
├── models/
│   └── sample_configurations.py
└── test/
    └── run_files_ee-opt.py

The utils directory contains various utility functions, and I'm trying to import them into the run_files_ee-opt.py script located in the test directory. Here's the relevant part of the run_files_ee-opt.py script:

from utils.custom_object_functions import create_hollow_room, get_arrow, create_coordinate_axes_mesh

# Rest of the code...

I'm running this script within a conda environment named apxcv. The sys.path includes the empty string '', which I understand should represent the current working directory.

However, when I try to run the script from the project root (apx-cv) using the command:

python test/run_files_ee-opt.py

I encounter the following error:

ModuleNotFoundError: No module named 'utils'

I also want to mention that earlier I have been running this script from PyCharm, and it works smoothly there. Now, I want to run it on a remote machine using terminal. When I run it using terminal, it does not work on the remote machine, or even my local machine.

The problem persists with all modules not just utils, because I wasn't able to import models either. I think I am missing something very basic, but I am not a very strong computer programmer, so I don't know much.

Things that I have tried:

  1. Verified sys.path: I printed sys.path inside the script, and it correctly includes the current working directory ('').
  2. Absolute Import: I ensured that I'm using absolute imports (not relative imports), so from utils.custom_object_functions import ... should be valid.
  3. Checked Working Directory: I confirmed that the working directory is the project root (apx-cv) when running the script.
  4. Also checked utils.utils is not making an issue, as I excluded all utils-related methods. Even then, I still cannot import models.

The script should run without the ModuleNotFoundError since the utils module is in the correct location, and sys.path appears to be set up correctly.


Solution

  • The sys.path includes the empty string '', which I understand should represent the current working directory.

    This is only true when you run python on a file that is in the local directory, like this:

    python myscript.py
    

    But you are running python with a script file in a different directory:

    python test/run_files_ee-opt.py
    

    So in this case, sys.path does not include the empty string, it includes the string 'test'.