ImportError with Relative Imports When Debugging a Multi-directory Python Project in VSCode

124 views Asked by At

I'm working on a Python project with multiple directories and facing an issue when trying to debug using VSCode. The project structure is as follows:

├── main_script.py
├── plots
│   ├── plot_script_1.py
│   └── plot_script_2.py
├── src
│   ├── __init__.py
│   ├── analysis
│   │   ├── __init__.py
│   │   └── analysis_module.py
│   ├── frameworks
│   │   ├── __init__.py
│   │   └── framework_module.py
│   └── utils
│       ├── __init__.py
│       └── utility_module.py
└── tests
    └── test_framework.py

In framework_module.py, I'm trying to import from other modules:

from ..analysis.analysis_module import dummy_analysis
from ..utils.utility_module import dummy_util

def framework_analysis(arguments):
    # Rest of the code...

Running main_script.py from the terminal works fine, but when I try to debug framework_module.py using breakpoints in VSCode, I encounter the following ImportError:

Exception has occurred: ImportError
attempted relative import with no known parent package
  File "{file}", line {line}, in <module>
    from ..analysis.analysis_module import dummy_analysis

This occurs even when I include a if __name__ == '__main__': block in framework_module.py for testing purposes.

What I've tried:

  • Setting up a __main__ block in framework_module.py and running it as a script.
  • Checking for incorrect paths or misspellings in the import statements.

I'm unsure how to resolve these import errors when debugging individual modules in a multi-directory project. How should I properly set up debugging in this scenario with VSCode?

I already checked upon the other issues related to relative imports here, but none of them seemed to work.

1

There are 1 answers

6
LittleNyima On

You may want to configure the launch options of vscode for Python scripts. Here is an example: create a file .vscode/launch.json under your working directory and its content should be like:

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Current File",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal",
            "justMyCode": false,
            "cwd": "/path/to/your/working/directory",
            "args": [
                "--some", "launch", "arguments"
            ],
            "env": {
                "ENVIRONMENT_VARIABLES": "SOME_VALUE"
            }
        }
    ]
}

This makes the debugging launch in vscode equivalent to run this command under /path/to/your/working/directory:

ENVIRONMENT_VARIABLES=SOME_VALUE python ${file} --some launch arguments

If something is still unclear, please refer to the official documentation for further information.