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文件"

相关推荐
白玉cfc19 小时前
【iOS】push,pop和present,dismiss
macos·ios·cocoa
ZZHow102420 小时前
02OpenCV基本操作
python·opencv·计算机视觉
计算机学长felix20 小时前
基于Django的“酒店推荐系统”设计与开发(源码+数据库+文档+PPT)
数据库·python·mysql·django·vue
站大爷IP20 小时前
Python随机数函数全解析:5个核心工具的实战指南
python
悟乙己20 小时前
使用 Python 中的强化学习最大化简单 RAG 性能
开发语言·python·agent·rag·n8n
max50060020 小时前
图像处理:实现多图点重叠效果
开发语言·图像处理·人工智能·python·深度学习·音视频
AI原吾20 小时前
玩转物联网只需十行代码,可它为何悄悄停止维护
python·物联网·hbmqtt
云动雨颤20 小时前
Python单元测试入门:3个核心断言方法,帮你快速定位代码bug
python·单元测试
编程乐学21 小时前
安卓非原创--基于Android Studio 实现的新闻App
android·ide·android studio·移动端开发·安卓大作业·新闻app
SunnyDays101121 小时前
Python 实现 HTML 转 Word 和 PDF
python·html转word·html转pdf·html转docx·html转doc