1

I created workspace_folder, cloned a github repo (folder name) which has repo_module folder, which in turn has train.py and algo.py:

workspace_folder
|
+-- repo
     |
     +-- train.py
     |
     +-- algo.py

The repo's readme asks to run:

python -m repo_module.train 

which I am able to run from within repo folder:

workspace_folder/repo$ python -m repo_module.train

But I want to debug the train.py in vscode. I opened train.py and started debugging with Python: Current File configuration. But it started giving me following error:

Exception has occurred: ImportError
attempted relative import with no known parent package

for line from . import algo in train.py.

Answer here suggests that we need to do some changes to the code. I was thinking if I can make any changes to vscode debug launch.json and make it work. I tried to set "cwd": "${workspaceFolder}/repo" in launch.json but it gave same error. So, for now I have replaced all from . (space after dot .) with empty string in all files (converting all from . import abc to import abc) and from . (no space after .) to from in all files(converting all from .abc to from abc) and am able to debug it. But I was able to guess if I can do away with modifying code and configuring in vscode launch.json. Is it possible.

(Also note that my changes seem to have worked as all py files are "directly" inside repo folder and there is no subfolder inside repo folder.)

Rnj
  • 1,067
  • 1
  • 8
  • 23
  • So basically I have to make changes to code merely for debugging? I cannot debug by specifying some config in vscode? – Rnj Apr 05 '21 at 18:07
  • @Rnj -When importing other files, VS Code looks for the file in the parent folder of the imported file by default, so please use "_import algo_" in the file "train.py". – Jill Cheng Apr 06 '21 at 08:53
  • 1
    Yeah ended up doing that only. But point is that I dont want to modify py files as there may be a lot of them. I felt there should be some config in vscode which will allow to fix it. – Rnj Apr 06 '21 at 10:57

0 Answers0