I have the following directory structure for my python practice project:
.
├── data
├── ds-and-algo
├── exercises
| ├── __init__.py
│ ├── armstrong_number.py
│ ├── extract_digits.py
├── output
The extract_digits.py looks something like this:
def extract_digits(n):
pass
In the armstrong_number.py I have the following:
from .extract_digits import extract_digits
From root project directory if I run
python exercises/armstrong_number.py
I get ModuleNotFoundError: no module named exercises
Running the following commad with -m flag resolves the error:
python -m exercises.armstrong_number
However using VSCode in order to debug the file, I have the following debug config launch.json:
{
"version": "0.2.0",
"configurations": [
{
"name": "Python Module",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"pythonPath": "${config:python.pythonPath}",
"module": "exercises.${fileBasenameNoExtension}",
"cwd": "${workspaceRoot}",
"env": {"PYTHONPATH":"${workspaceRoot}"}
}
]
}
However this has a few problems:
1) For a different folder, for e.g. ds-and-algo, I have to manually edit the module entry in the launch.json file debug configuration to
"module" : "ds-and-algo.${fileBaseNameNoExtension}"
In case I have nested folder configuration like:
exercises
├── tough
| | __init__.py
| ├──ex1.py
| ├──ex2.py
├── easy
I again have to manually edit the debug config in launch.json file to: (considering the case for the sub-folder tough)
"module": "exercises.tough.${fileBaseNameNoExtension}"
I need to implement a general case where depending on the file being debugged, the "module" entry in the launch.json file should be:
"module": "folder1.folder2.folder3.....foldern.script"
Just like fileBaseNameNoExtension, VSCode has some other predefined variables:
One of the variables is relativeFile, which is the path of the current opened file relative to workspaceFolder
So for the file ex1.py, the variable relativeFile will be exercises/tough/ex1.py.
I need to manipulate this string and convert this to exercises.tough.ex1, which is trivial if I can write and execute bash command inside the "module" entry in launch.json file. But I am unable to do that. However, the link predefined variables in VSCode has a section on Command variables, which states:
if the predefined variables from above are not sufficient, you can use any VS Code command as a variable through the ${command:commandID} syntax.
This link has a bunch of other information that may be helpful. I am no expert in python, and definitely don't know any javascript, if that is what is required to solve this problem.