[英]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.使用 VSCode,我在虚拟环境中调试时遇到了一个问题,该环境具有未安装在
base
环境中的不同包。 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
.使用命令
activate my_env
激活环境后,我可以使用通常的 python 命令作为python main.py
使用环境中的包。 But while debugging, I can't use the packages which are only installed in my_env
.但是在调试时,我不能使用只安装在
my_env
中的包。 How can I debug a python code in a virtual environment using VSCode?如何使用 VSCode 在虚拟环境中调试 python 代码?
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.我正在尝试将
xmltodict
包与使用 Visual Studio 代码的简单代码一起使用。
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.我有一个名为
my_env
的虚拟环境,并在其中安装了xmltodict
包。 When I activate the environment with activate my_env
, this code works fine.当我使用
activate my_env
激活环境时,此代码工作正常。 But, when I try to use vscode debug option, it gives No module named 'xmltodict'
error.但是,当我尝试使用 vscode调试选项时,它会给出
No module named 'xmltodict'
错误。 Becuase vscode debug button opens new cmd and run the debugging command in it, I stopped debugging and typed activate my_env
in that cmd and tried to debug again, but still it can't find the module.因为 vscode 调试按钮打开新的 cmd 并在其中运行调试命令,我停止调试并在该 cmd 中键入
activate my_env
并尝试再次调试,但仍然找不到模块。 Also, I tried jupiter notebook in vscode, it also doesn't see the package.另外,我在 vscode 中尝试了 jupiter notebook,它也没有看到包。
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.我看到
import xmltodict
在 vscode 中带有红色下划线,它说Unable to import 'xmltodict'
,但是当我从 cmd 正常运行它时它可以工作。 This happens sometimes for other modules and I don't know why.有时其他模块会发生这种情况,我不知道为什么。 I installed
xmltodict
module using pip
, maybe it causes that.我使用
pip
安装了xmltodict
模块,也许是它导致的。
I am using Visual Studio Code 1.30.1 with Anaconda Python 3.7.1 on Windows 10.我在 Windows 10 上使用 Visual Studio Code 1.30.1 和 Anaconda Python 3.7.1。
How can I debug a python code in a virtual environment using VSCode?如何使用 VSCode 在虚拟环境中调试 python 代码? I saw this question, but I don't think it is exactly what I want?
我看到了这个问题,但我不认为这正是我想要的?
将"python.pythonPath"
设置为指向您要使用的虚拟环境中的解释器。
I am using venv for creating virtualenv, and VS code to debug the code.我正在使用 venv 来创建 virtualenv,并使用 VS 代码来调试代码。
I found we don't have to create a lanuch.json file but added settings.json under {project}/.vscode/ folder worked as well.我发现我们不必创建 lanuch.json 文件,但在 {project}/.vscode/ 文件夹下添加 settings.json 也能正常工作。 My settings.json is as below:
我的 settings.json 如下:
{
"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.
希望它会帮助你。
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.使用 Python:从命令面板中选择解释器命令 (Ctrl+Shift+P),然后选择属于新虚拟环境的 Python 解释器。
If you are using a virtual env on linux
on the drop down, select env
> bin
> python
ie env/bin/python
如果您在
linux
上使用虚拟环境,请在下拉菜单中选择env
> bin
> python
即env/bin/python
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.