vscode运行Python失败:F5无法运行程序和设置断点debug

一、概述

经常遇到这个问题,特记录解决方法,供后续遇到同样问题时查阅。

二、解决方法

在当前根目录创建.vscode文件夹,文件夹里创建launch.json和setting.json两个文件。

1、设置launch.json

{
    // 使用 IntelliSense 了解相关属性。 
    // 悬停以查看现有属性的描述。
    // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Current File",
            "type": "python",
            "request": "launch",
            "stopOnEntry": false,
            "python": "/home/xxxx/anaconda3/envs/torch/bin/python",
            "program": "${file}",
            "cwd": "${workspaceRoot}",
            "console": "integratedTerminal",
            "justMyCode": true
        }
    ]
}

2、设置setting.json

{
    "python.pythonPath": "/home/xxxx/anaconda3/envs/torch/bin/python",
}

3、配置解释器环境

(1)点击界面左下角齿轮【管理】-> Command Palette 【命令面板】;
(2)输入 Python:Select Interpreter;
(3)选择指定的conda虚拟环境。

大功告成!

### 配置 VSCode 调试 Python 3.7 要在 Visual Studio Code (VSCode) 中设置 Python 3.7 的调试环境,需完成以下几个方面的配置: #### 1. 安装 Python 3.7 确保已经正确安装了 Python 3.7。可以通过以下命令验证是否成功安装并确认版本号: ```bash python3.7 --version ``` 如果尚未安装,则可以按照以下方法进行安装[^3]: ```bash sudo apt-get update sudo apt-get install python3.7 sudo apt-get install python3.7-dbg curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py sudo python3.7 get-pip.py ``` #### 2. 安装必要的工具依赖项 为了实现代码格式化静态分析功能,建议安装 `pycodestyle` `yapf` 工具: ```bash sudo pip3.7 install pycodestyle yapf ``` #### 3. 配置 VSCodePython 插件 在 VSCode 中打开工作区后,按快捷键 `Ctrl+Shift+P` 打开命令面板,输入 **Python: Select Interpreter** 并选择对应的 Python 3.7 解释器。 #### 4. 修改 settings.json 文件 编辑 `.vscode/settings.json` 文件以指定 Python 版本其他相关选项。以下是推荐的配置内容[^1]: ```json { "python.pythonPath": "python3.7", "python.formatting.provider": "yapf", "python.linting.flake8Path": "pycodestyle", "python.linting.flake8Enabled": true } ``` 注意:如果是在 Windows 系统上运行,可能需要调整路径为绝对路径形式,例如[^5]: ```json "python.pythonPath": "C:/Users/Administrator/AppData/Local/Programs/Python/Python37/python" ``` #### 5. 创建 launch.json 文件 通过按下 `F5` 或者点击左侧边栏中的“Run and Debug”图标来创建一个新的调试配置文件 (`launch.json`)。对于标准的 Python 应用程序,可以选择模板 “Python”,这会自动生成如下结构的基础框架: ```json { "version": "0.2.0", "configurations": [ { "name": "Python: Current File", "type": "python", "request": "launch", "program": "${file}", "console": "integratedTerminal", "justMyCode": false } ] } ``` 在此基础上可以根据需求进一步定制参数,比如启用或禁用 `justMyCode` 来控制是否跟踪进入外部库函数内部逻辑[^4]。 #### 6. 测试调试环境 保存所有更改之后,在任意 .py 文件中右击菜单选择 “Debug Current File in Python Terminal”。此时应该能够顺利启动基于所选解释器的调试过程,并利用断点等功能逐步排查问题所在。 尽管 Microsoft 正逐渐减少对较旧版 Python 如 3.7 的全面支持,上述步骤依然适用于大多数常规场景下的项目开发维护任务。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值