skip to Main Content

I have some Python source code and want to debug this code via VS Code.

I set-up a virtual environment through Ctrl+Shift+P->Select Interpreter, and this virtual environment is shown in the right-bottom corner of VS Code. If I execute Terminal->New Terminal command, the new terminal would be opened with venv activated.

But If I press F5 or Ctrl+F5, another terminal would be opened, and in this terminal venv would not be activated. I have to stop debugging, activate venv manually by

source myvenv/bin/activate

and press F5 again. Now I can debug using venv.

Do I can make VS Code open terminal for debug with venv activated?

This is an example of my launch configuration:

{
    "name": "MyConfiguration",
    "type": "debugpy",
    "request": "launch",
    "program": "test.py",
    "console": "integratedTerminal",
    "justMyCode": false
}

Among other things, I use subprocess.run(..) in my Python code and want this subprocess to be executed with venv activated.

2

Answers


  1. Chosen as BEST ANSWER

    Solved by adding to .bashrc

    [[ "$TERM_PROGRAM" == "vscode" ]] && . "$(code --locate-shell-integration-path bash)"
    

    https://github.com/microsoft/vscode-python/issues/22860


  2. In the launch configuration, point to the python interpreter within your env. This should activate your venv automatically while debugging.
    Try this:

       {
            "name": "MyConfiguration",
            "type": "python",
            "request": "launch",
            "program": "test.py",
            "pythonPath": "venv_Path/bin/python",
            "console": "integratedTerminal",
            "justMyCode": false
        }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search