Mac Pycharm在Debug模式报编码(SyntaxError)错误

1. 错误信息:

复制代码
Traceback (most recent call last):
  File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/tokenize.py", line 330, in find_cookie
    line_string = line.decode('utf-8')
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xca in position 0: invalid continuation byte

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/tokenize.py", line 394, in open
    encoding, lines = detect_encoding(buffer.readline)
  File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/tokenize.py", line 371, in detect_encoding
    encoding = find_cookie(first)
  File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/tokenize.py", line 335, in find_cookie
    raise SyntaxError(msg)
SyntaxError: invalid or missing encoding declaration for '/Users/willow/WorkSpaces/PycharmProjects/panda-hub/venv/bin/Python'

2. 解决思路

试过很多方案,什么在文件头加utf8编码,或者idea设置编码为utf8,结果都没用,偶然发现在脚本环境运行和非debug模式运行都没有问题,那么肯定就是debug的问题,所以看了配置。发现了问题所在。

3. 解决方案

去掉这个 Attach to subprocess automatically while debugging

官方解释: If this checkbox is selected, PyCharm will automatically attach all subprocesses of the process being debugged. Thus, if the parent process has subprocesses, their breakpoints will always work.

中文:如果选中此复选框,PyCharm将自动附加正在调试的进程的所有子进程。因此,如果父进程有子进程,它们的断点将始终有效。

白话:debug方式启动的时候,是多进程的,看下面的命令:

复制代码
venv/bin/Python /Applications/PyCharm.app/Contents/plugins/python/helpers/pydev/pydevd.py --multiprocess --qt-support=auto --client 127.0.0.1 --port 55207 --file /Users/willow/WorkSpaces/PycharmProjects/panda-hub/main.py 

也就是说,这个编码错误实在运行子进程的时候解释代码导致的。那么为什么在非debug模式没有这个问题? 因为"pyc文件"

相关推荐
岁月宁静1 小时前
LangGraph 技术详解:基于图结构的 AI 工作流与多智能体编排框架
前端·python·langchain
百锦再1 小时前
京东云鼎入驻方案解读——通往协同的“高架桥”与“快速路”
android·java·python·rust·django·restful·京东云
岁月宁静1 小时前
LangChain 技术栈全解析:从模型编排到 RAG 实战
前端·python·langchain
Nick_zcy1 小时前
基于Vue和Python的羽毛球拍智能推荐系统, 从“不会选羽毛球拍”到“选对拍”的一站式小工具
前端·vue.js·python·算法·推荐算法
冰冰菜的扣jio2 小时前
理解类加载过程
开发语言·python
qilei20102 小时前
【Python】创建日期列表
python
百***07452 小时前
GPT-5.2国内稳定接入实战指南:中转调用全链路方案(Python适配)
python·gpt·php
zyxqyy&∞2 小时前
python代码小练-4
开发语言·python
luoluoal2 小时前
基于python的反爬虫技术的研究(源码+文档)
开发语言·python·mysql
老歌老听老掉牙2 小时前
圆柱立铣刀容屑槽几何要素仿真及计算分析
python·立铣刀·容屑槽