Mac Pycharm在Debug模式报编码(SyntaxError)错误 python3.9/tokenize.py", line 330, in find_cookie

作者:じ☆ve宝贝

发布时间:2024-06-01T01:01:46

最近更换macos系统后,发现在pycharm中debug运行项目就会出现python3.9/tokenize.py", line 330, in find_cookie错误,直接run可以正常运行。

错误信息

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'

解决方案

image.png

原因复盘

去掉的这个: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将自动附加正在调试的进程的所有子进程。因此,如果父进程有子进程,它们的断点将始终有效。简单来说就是这个编码错误是在运行子进程的时候解释代码导致的。