vscode debug python2.7报错

在使用VSCode调试Python2.7代码时遇到错误,具体表现为无法正常运行。解决方法是在`pydevd_api`文件顶部添加特定代码段。

vscode调试python2.7相关的文件时候,报如下错误:

Traceback (most recent call last):
  File "/home/yinshuai/.vscode/extensions/ms-python.python-2019.5.18875/pythonFiles/lib/python/ptvsd/_vendored/pydevd/_pydevd_bundle/pydevd_trace_dispatch_regular.py", line 414, in __call__
    file_type = py_db.get_file_type(abs_path_real_path_and_base)  # we don't want to debug threading or anything related to pydevd
  File "/home/yinshuai/.vscode/extensions/ms-python.python-2019.5.18875/pythonFiles/lib/python/ptvsd/_vendored/pydevd/pydevd.py", line 635, in get_file_type
    file_type = PYDEV_FILE if self.dont_trace_external_files(abs_real_path_and_basename[0]) else None
  File "/home/yinshuai/.vscode/extensions/ms-python.python-2019.5.18875/pythonFiles/lib/python/ptvsd/_vendored/pydevd/_pydevd_bundle/pydevd_api.py", line 577, in custom_dont_trace_external_files
    return abs_path.startswith(start_patterns) or abs_path.endswith(end_patterns)

找到文件pydevd_api,在文件头部添加如下部分: 

### 配置 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 的全面支持,上述步骤依然适用于大多数常规场景下的项目开发维护任务。
评论 3
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值