Python 调试器在 venv 下无法在 VSCode 中工作

吉米特1988

我有以下 settings.json

{
    "python.pythonPath": "C:\\Users\\james\\OneDrive\\Programming Projects\\Python\\webscraper\\Scripts\\python.exe",
    "python.linting.enabled": true,
    "python.linting.pylintEnabled": true,
    "python.linting.pylintPath": "\\Scripts\\pylint.exe"
}

以及launch.json中的以下条目:

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Current File (Integrated Terminal)",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal"
        }
    ]
}

当我转到我想调试并选择我的Python: Current File (Integrated Terminal)调试器的文件时,它会显示我的断点并显示以下内容:

在此处输入图片说明

另一方面,如果我设置了一个新的解释器,例如:

"C:\\Users\\james\\AppData\\Local\\Programs\\Python\\Python39-32\\python.exe"

它将运行调试器,但同时,它不会考虑我的 venv,因此会引发有关缺少模块的错误(应该如此)。

在此处输入图片说明

你知道我可能哪里出错了吗?

吉米特1988

这里有几点要提:

The environment should not have your files inside it, it's supposed to be something you use and throw away at a whim. Place project files outside of the environment you create. I laid mine out as a folder called "env" that I created using:

py -m venv env

and then after it was created, I activated it with

env/Scripts/activate

在此处输入图片说明

Second of all, don't forget to ensure your vscode folder is in that root folder, here are my settings for my configuration that ended up working.

在此处输入图片说明

This seemed to be really important:

"python.linting.pylintArgs": [
    "--init-hook",
    "import sys; sys.path.append('C:\\Users\\james\\OneDrive\\Programming Projects\\Python')"
],

And just in case it wasn't clear how my folder structure now looks like. the main.py, logic, models and services folders are my own code. The env folder is the environment created with venv:

在此处输入图片说明

Just an extra bonus, don't forget to create a requirements.txt that stores libraries you use in your project

在此处输入图片说明

然后,您可以使用以下方法安装:

py -m pip install -r requirements.txt

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章