0

I want to debug a python module in vscode to save time and help me figure out what's going on with the code. But I'm having two problems (I'll focus more on the 1st issue for this post) that seem like they're related to me not using launch.json correctly and I would like to know what's going wrong. I'm working on a Mac.

My module is called __main__. The full absolute path to it would be ~/Code/APPRES-483/cqs/update-query-definition/__main__.py (__main__.py is a script, but it seems the module is called __main__ as well). There's a virtualenv at ~/Code/APPRES-483/venv-cqs/ that I'm using. I opened up a VSCode window inside of ~/Code/APPRES-483/cqs/update-query-definition/.

Right now my launch.json file looks like this.

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Module",
            // "python":"~/Code/APPRES-483/venv-cqs/bin/python",
            "type": "python",
            "request": "launch",
            "module": "__main__",
            "pythonArgs":[
                "-v",
                "--file-to-write", "~/Code/APPRES-483/LOCAL-query-definition.yml"
            ],
        },

    ]
}

The first problem is that I can't actually set the python interpreter path in launch.json. I have my virtualenv active inside of VSCode (i.e. I see the prefix for venv-cqs that I intend to see).

If I open up a terminal and put ~/Code/APPRES-483/venv-cqs/bin/python, that correctly opens up the python terminal/executable inside of venv-cqs/bin. If I add a line like "python":"~/Code/APPRES-483/venv-cqs/bin/python", I get the message saying "the python path in your debug configuration is invalid."

I tried to follow https://stackoverflow.com/a/66885651/6432861 instructions to use the Python: Select Interpreter command. I'm getting some weird behavior.

  • If I try to browse finder/files to find the python executable in venv-cqs/bin/, once I double click on the file it doesn't actually get made into the python interpreter for vs code. If I hover my mouse in the bottom left, I'm still seeing the address of my default python installation in /usr/local/bin or wherever it is.

  • But if I copy and paste ~/Code/APPRES-483/venv-cqs/bin/python rather than trying to browse for files, that successfully changes the python interpreter and I can see that at the bottom left of my screen.

vs code showing the correct python interpreter

The only way that the code avoids these errors is if I don't have a python field in the configuration for launch.json. I know that's not how it's supposed to be so... I want to know what's going wrong.

The second problem I'm having is with pythonArgs. But I'll try to make another post about it since this is already long enough.

3
  • ~ expansion is a shell feature. It is not guaranteed that it works somewhere else. Commented Sep 28, 2021 at 20:34
  • Then what is the syntax to do an expansion of the home address in vscode? I can't find anything that says a tilde wouldn't work and also the explorer tooltip on the lefthand side for file uses a tilde expansion as well. Commented Sep 28, 2021 at 21:23
  • @KlausD. I just found that using the full expanded path (eg. /Users/bineyb/Code/.... rather than a ~ seems to fix the issue. It's the same problem discussed in this github ticket, but allegedly they should have fixed it to make the tilde work properly (and it doesn't for me): github.com/microsoft/vscode/issues/32736 Commented Sep 30, 2021 at 15:43

1 Answer 1

0

First, you may set

 "python.defaultInterpreterPath": "~/Code/APPRES-483/venv-cqs/bin/python",

in User Settings.json to specify python interpreter. If it doesn't work and VS Code still shows default python installation as python interpreter in bottom-left corner, deleting the user data folders $HOME/Library/Application Support/Code and .~/.vscode can reset VS Code.

Second, the setting python defaults to the interpreter selected for your workspace, so once you have selected the venv-cqs as python interpreter, debug would use it by default and if you insist that, set

"python": "${command:python.interpreterPath}"
Sign up to request clarification or add additional context in comments.

2 Comments

I think this worked. Now when I use the Select Interpreter command, I have the option to use Python from defaultInterpreterPath setting. But I'm still confused, what was wrong with the way I was declaring python previously with "python":"~/Code/APPRES-483/venv-cqs/bin/python"? Why wasn't that a valid path?
@ByronSmith. I'm using Windows and full path is okay, maybe wrong path formatting?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.