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

相关推荐
一次旅行8 分钟前
act本地预跑GitHub Actions:Python项目完整CI/CD流水线实战
python·ci/cd·github
玉鸯17 分钟前
RAG 全链路调优指南:从 Chunking 到 Reranker
python·llm·agent
圣光SG30 分钟前
Java操作题练习(二)
java·开发语言·python
m0_6174939431 分钟前
Python OpenCV 分水岭算法(Watershed)详解与实战
python·opencv·算法
Sammyyyyy32 分钟前
如何利用本地技术栈构建 0 成本 AI SaaS 雏形
开发语言·人工智能·python·ai·servbay
AAIshangyanxiu36 分钟前
Python 机器学习与深度学习气象水文海洋全域应用技术体系:地学时空数据 AI 建模、数值模式后处理、气象海洋水文智能预测与数据处理
人工智能·python·机器学习·水文气象·气象预测·气象海洋
中微极客43 分钟前
边缘AI实战:TinyML模型量化与部署全解析(TensorFlow 2.18.0 + ESP32)
人工智能·python·tensorflow
Zane19941 小时前
可变对象 vs 不可变对象:为什么"可变默认参数"是 Python 最经典的坑?
后端·python
cts6181 小时前
Python全栈claude.md文档
开发语言·python
名字还没想好☜1 小时前
Spring @Async 不生效排查:自调用失效、默认线程池坑与异步方法里的异常去哪了
java·python·spring·异步