简体   繁体   English

如何使用 VSCode 在虚拟环境中调试 python 代码?

[英]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 > pythonenv/bin/python

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python:无法在虚拟环境中使用vs代码进行调试 - Python: Cannot debug using vs code using virtual environment 如何为Python指定虚拟环境 - How can I specify virtual environment for Python 如何在使用脚本的调试模式(Linux机器)时在pycharm中激活wsl中的虚拟环境 - How Can I activate virtual environment in wsl in pycharm while using debug mode of a script (Linux machine) 我无法在 vscode 中运行代码或调试我的 python 文件 - I can't run code or debug of my python file in vscode 如何使用 python 在 VSCode 上调试 Google Code Jam 交互问题? - How to debug Google Code Jam interactive problems on VSCode using python? 我可以通过 vscode 在带有虚拟环境的 python 文件夹下创建文件并“正常工作”吗? - Can I just create files and “work as normal” under a python folder with a virtual environment on it via vscode? 如何在 Visual Studio Code 中为 Python 设置虚拟环境? - How can I set up a virtual environment for Python in Visual Studio Code? 当我安装了多个 python 版本时,如何为 VSCode 虚拟环境指定 python 版本? - How to specify the python version for VSCode virtual environment, when I have multiple python versions intalled? 我怎样才能知道我正在使用哪个 python 虚拟环境? - how can I find out which python virtual environment I am using? Visual Code 如何自动激活虚拟环境? - Visual Code how can I activate the virtual environment automatically?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM