How can I debug a python code in a virtual environment using VSCode?
Asked Answered
C

6

47

EDIT

Using VSCode, I had an issue while debugging in a virtual environment that have different packages which are not installed in the base environment. After activating the environment with the command activate my_env, I can use the packages in the environment with usual python command as python main.py. But while debugging, I can't use the packages which are only installed in my_env. How can I debug a python code in a virtual environment using VSCode?

This is this the summary of the question. The rest has some specific info about my case.

BEFORE EDIT

I am trying to use xmltodict package with a simple code using visual studio code.

import xmltodict

with open('C:\\Users\\user\\foo.xml') as f:
    db_dict = xmltodict.parse(f.read())

print(db_dict)

I have a virtual environment named my_env, and I installed xmltodict package in it. When I activate the environment with activate my_env, this code works fine. But, when I try to use vscode debug option, it gives No module named 'xmltodict' error. Becuase vscode debug button opens new cmd and run the debugging command in it, I stopped debugging and typed activate my_envin that cmd and tried to debug again, but still it can't find the module. Also, I tried jupiter notebook in vscode, it also doesn't see the package.

I see that import xmltodict is underlined with red in vscode and it says Unable to import 'xmltodict', but it works when I run it normally from cmd. This happens sometimes for other modules and I don't know why. I installed xmltodict module using pip, maybe it causes that.

I am using Visual Studio Code 1.30.1 with Anaconda Python 3.7.1 on Windows 10.

How can I debug a python code in a virtual environment using VSCode? I saw this question, but I don't think it is exactly what I want?

Chafin answered 2/1, 2019 at 15:35 Comment(2)
does this help: code.visualstudio.com/docs/python/environments ?Ushijima
Yes it is helpful. I tried a couple things from that documentation but I couldn't find the exact answer in it. Still, I can't debug my code.Chafin
T
50

Make sure the environment you want to use is selected in the Python extension for VS Code by running the Select Interpreter command or via the status bar. Otherwise you can explicitly set the Python interpreter to be used when debugging via the python setting for your debug config.

Tourane answered 3/1, 2019 at 23:0 Comment(8)
Thanks. If we add python.pythonPath to settings.json, it works. But, I just want to add another way which is making the same thing. Use the Python: Select Interpreter command from the Command Palette (Ctrl+Shift+P) and select the python interpreter that belongs to the new virtual environment. I tried this one at the beginning but I couldn't see a python interpreter for new environment in the selection list. Restarting VSCode fixed the situation fro me. After restart, I could see the python interpreter for the new environment in the selection list.Chafin
@Chafin glad it worked! Yes, the Select Interpreter command actually writes out the setting for you. And the newest release of the extension should detect a new virtual environment in the directory, but we have to notice it which isn't necessarily immediate.Tourane
@Chafin I directly used your method and worked like a charm! Thanks ~Am‚lie
That explains alot. (sarcasm detected)Sycosis
It is unclear in which file to put the python path key value store in your answer. I guess it should go in the project local settings.json.Hewart
for me in vscode v1.66.1 I had to add additional flag "justMyCode": false in my ./.vscode/launch.json so it would work properly, after this flag, it started working and I was able to debug libraries also :)Rest
explicitly setting the python interpreter: code.visualstudio.com/docs/python/…Fumigate
Works with code-server as well.Upsydaisy
H
7

Use the Python: Select Interpreter command from the Command Palette (Ctrl+Shift+P) and select the python interpreter that belongs to the new virtual environment.

If you are using a virtual env on linux on the drop down, select env>bin>python i.e env/bin/python

Hatten answered 23/1, 2022 at 5:13 Comment(0)
C
2

I am using venv for creating virtualenv, and VS code to debug the code.

I found we don't have to create a launch.json file but add settings.json under {project}/.vscode/ folder. My settings.json is as below:

{
"python.testing.unittestArgs": [
    "-v",
    "-s",
    ".",
    "-p",
    "test_*.py"
],
"python.testing.pytestEnabled": false,
"python.testing.nosetestsEnabled": false,
"python.testing.unittestEnabled": true,
"python.pythonPath": "/Users/hhh/project/bin/python"
}

I can debug the project and run the unit test as well. Hope it will help you.

Caulicle answered 31/3, 2020 at 8:28 Comment(0)
C
2

Menubar -> View -> Command Palette -> Python: Select Interpreter

Screenshot

Collector answered 17/7, 2023 at 8:4 Comment(2)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Evonevonne
While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From ReviewLaminar
M
1

Set justMyCode to false

I set a breakpoint inside a Python file in my virtual environment. I hovered with my house over that breakpoint in the breakpoint panels, and I saw this tooltip:

Breakpoint in file excluded by filters.

Note: may be excluded because of "justMyCode" option (default == true). Try setting "justMyCode": false in the debug configuration (e.g: launch.json).

So I opened launch.json, and I modified it to look like this:

{
    "version": "0.2.0",
    "configurations": [
        {
            // ...
            "justMyCode": false
        }
     ]
}
Metacenter answered 14/6 at 8:49 Comment(0)
H
0

Two steps for the venv python debug

  1. in the project local settings.json:

"python.pythonPath":"venv/bin/python"

  1. On the left in the debug side panel: Click "create launch.json" below the blue "Run and Config" button A project local launch.json with python is created:

    { "name": "Python", "type": "python", "request": "launch", "program": "${file}", }

  2. Press F5 and shoot

Hewart answered 30/3, 2022 at 16:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.