0

I have a project structured something like this:

project_folder
    |--configuration
        |--__init__.py
        |--config.py
        |--info.py
    |--data
    |--src
        |--models
            |--model.py
        |--scripts
            |--collect.py
        |--tools
            |--__init__.py
            |--utils.py
            |--process.py
        |--train
            |--trainModel.py
    |--server.py

The config file in /configuration depends a list inside info.py so it is imported to the config file with import info.

When I try to import the config file to the utils file in /src/tools with import configuration.config as con, I get the error ModuleNotFoundError: No module named 'configuration'.

When I add sys.path.append(os.path.join(os.path.dirname(__file__), '..', '..')), I then get the error ModuleNotFoundError: No module named 'info' instead of the previous one.

Is there a way to change the import statements in the utils file to resolve these issues?

  • Unless you've created an installable package, the location of modules is sensitive to your current working directory. – erip Jul 07 '23 at 20:40
  • Not current working directory, but the directory that contains the *script* that's doing all the importing. When you run `foo/bar.py`, `foo` is added to `sys.path`, and top-level modules need to be found in `foo` or some other directory on `sys.path`. – chepner Jul 07 '23 at 21:21
  • @chepner So would I need to run /src/tools/utils from from the /project directory? Also, should I keep the `sys.path.append(os.path.join(os.path.dirname(__file__), '..', '..')) ` or replace it with something else? – jon gluestick Jul 08 '23 at 02:10
  • When in doubt with relative imports, I always go here: [Relative imports for the billionth time](https://stackoverflow.com/questions/14132789/relative-imports-for-the-billionth-time) – spenpal Jul 08 '23 at 17:15

0 Answers0