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

相关推荐
多米Domi0112 小时前
0x3f 第49天 面向实习的八股背诵第六天 过了一遍JVM的知识点,看了相关视频讲解JVM内存,垃圾清理,买了plus,稍微看了点确定一下方向
jvm·数据结构·python·算法·leetcode
人工智能训练7 小时前
【极速部署】Ubuntu24.04+CUDA13.0 玩转 VLLM 0.15.0:预编译 Wheel 包 GPU 版安装全攻略
运维·前端·人工智能·python·ai编程·cuda·vllm
yaoming1687 小时前
python性能优化方案研究
python·性能优化
码云数智-大飞8 小时前
使用 Python 高效提取 PDF 中的表格数据并导出为 TXT 或 Excel
python
VernonJsn9 小时前
visual studio 2022的windows驱动开发
ide·驱动开发·visual studio
阿尔帕兹9 小时前
Visual Studio2019/2022离线安装完整教程(含闪退解决方法)
ide·visual studio
萌止9 小时前
Visual Studio下载保姆级安装教程
ide·visual studio
GISer_Jing9 小时前
解决Visual Studio调试时找不到exe文件的终极指南
ide·visual studio
程序人生5189 小时前
Visual Studio 2026下载与安装详细教程!(附下载地址)
ide·visualstudio·visual studio
biuyyyxxx10 小时前
Python自动化办公学习笔记(一) 工具安装&教程
笔记·python·学习·自动化