如何使用VSCode在虚拟环境中调试python代码? [英] How can I debug a python code in a virtual environment using VSCode?

查看:830
本文介绍了如何使用VSCode在虚拟环境中调试python代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑

使用VSCode,我在虚拟环境中进行调试时遇到了一个问题,该环境中的其他软件包没有安装在base环境中.使用命令activate my_env激活环境后,我可以将环境中的软件包与通常的python命令一起使用为python main.py.但是在调试时,我不能使用仅安装在my_env中的软件包.如何使用VSCode在虚拟环境中调试python代码?

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.

编辑前

我正在尝试使用Visual Studio代码的简单代码来使用xmltodict软件包.

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)

我有一个名为my_env的虚拟环境,并且在其中安装了xmltodict软件包.当我使用activate my_env激活环境时,此代码可以正常工作.但是,当我尝试使用vscode debug 选项时,它给出了No module named 'xmltodict'错误.因为vscode debug按钮会打开新的cmd并在其中运行debug命令,所以我停止了调试,并在该cmd中键入activate my_env并尝试再次调试,但仍然找不到该模块.此外,我在vscode中尝试了木星笔记本,但它也没有看到包装.

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.

我看到vsccode中的import xmltodict用红色下划线标出,并显示Unable to import 'xmltodict',但是当我从cmd中正常运行它时,它可以工作.有时其他模块也会发生这种情况,我不知道为什么.我使用pip安装了xmltodict模块,也许是导致这种情况的原因.

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.

我在Windows 10上将Visual Studio Code 1.30.1与Anaconda Python 3.7.1结合使用了.

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

如何使用VSCode在虚拟环境中调试python代码? 我看到了这个问题,但是我不认为这正是我想要的吗?

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?

推荐答案

设置"python.pythonPath"指向要使用的虚拟环境中的解释器.

Set "python.pythonPath" to point to the interpreter in the virtual environment that you want to use.

这篇关于如何使用VSCode在虚拟环境中调试python代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆