Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 792
Alperen Cetin Avatar asked Jan 02 '19 15:01

Alperen Cetin


People also ask

Can you debug Python in VS Code?

Python debugging in VS Code. The Python extension supports debugging of several types of Python applications. For a short walkthrough of basic debugging, see Tutorial - Configure and run the debugger. Also see the Flask tutorial.

How do I debug a code in VS Code?

To bring up the Run and Debug view, select the Run and Debug icon in the Activity Bar on the side of VS Code. You can also use the keyboard shortcut Ctrl+Shift+D. The Run and Debug view displays all information related to running and debugging and has a top bar with debugging commands and configuration settings.


3 Answers

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.

like image 183
Brett Cannon Avatar answered Oct 21 '22 18:10

Brett Cannon


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

like image 33
chia yongkang Avatar answered Oct 21 '22 17:10

chia yongkang


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.

like image 40
maoyang Avatar answered Oct 21 '22 17:10

maoyang