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.
You may want to configure the launch options of vscode for Python scripts. Here is an example: create a file
.vscode/launch.jsonunder your working directory and its content should be like:This makes the debugging launch in vscode equivalent to run this command under
/path/to/your/working/directory:If something is still unclear, please refer to the official documentation for further information.