How can I debug a python code in a virtual environment using VSCode?
Solution 1
Set "python.pythonPath"
to point to the interpreter in the virtual environment that you want to use.
Solution 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.
Comments
-
Alperen almost 2 years
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 commandactivate my_env
, I can use the packages in the environment with usual python command aspython main.py
. But while debugging, I can't use the packages which are only installed inmy_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 installedxmltodict
package in it. When I activate the environment withactivate my_env
, this code works fine. But, when I try to use vscode debug option, it givesNo module named 'xmltodict'
error. Becuase vscode debug button opens new cmd and run the debugging command in it, I stopped debugging and typedactivate my_env
in 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 saysUnable 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 installedxmltodict
module usingpip
, 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?
-
Alperen over 5 yearsThanks. If we add
python.pythonPath
tosettings.json
, it works. But, I just want to add another way which is making the same thing. Use thePython: 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. -
Brett Cannon over 5 years@Alperen 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. -
Burakhan Aksoy about 3 years@Alperen I directly used your method and worked like a charm! Thanks ~
-
Ilya Pukhov over 2 yearsThat explains alot. (sarcasm detected)
-
Timo about 2 yearsIt 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.
-
simkusr about 2 yearsfor 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 :)