UnicodeDecodeError: 'utf-8' when debugging Python files in PyCharm Community
UnicodeDecodeError: 'utf-8' when debugging Python files in PyCharm Community
当前结论:
converted
文件的encoding
为utf-8
->utf-8 big
->ansi
->utf-8
。 每次转换后重新打开文件。
观察了一段时间,没有这个错误。
我用PyCharm调试.py
文件时,same
文件有时会出现UnicodeDecodeError,有时又正常。我的操作系统是Windows10,PyCharm版本是2020.3.3社区版。
错误如下:
Traceback (most recent call last):
File "D:\Program Files\JetBrains\PyCharm Community Edition 2020.3.3\plugins\python-ce\helpers\pydev\_pydevd_bundle\pydevd_comm.py", line 301, in _on_run
r = r.decode('utf-8')
UnicodeDecodeError: 'utf-8' codec can't decode bytes in position 1022-1023: unexpected end of data
我尝试在文件头添加如下代码,但有时还是报错,如何解决?
#!/usr/bin/env Python
# coding=utf-8
我找到了另一种使用记事本另存为 UTF-8 文档的方法。我试过了,但有时还是会报错。
添加'ignore'。成功了。
r = r.decode('utf-8', 'ignore')
问题中描述的问题没有一个单一的答案。许多问题都可能导致指示的错误,因此最好在 PyCharm IDE.
的上下文中解决几个可能的因素
每个 Python 文件 .py
(或与此相关的任何其他文件)都有编码。 .py
源代码文件的默认编码是 Unicode UTF-8。这个问题是初学者经常遇到的,所以我们从官方文档中找出相关引述(以减少不必要的阅读时间):
The default encoding for Python source code is UTF-8, so you can simply include a Unicode character in a string literal.
这意味着在大多数情况下您不需要编码字符串,请参阅 Python Source Code Encodings - PEP 263。目前的做法是将源文件默认编码为 UTF-8 并省略模块顶部的编码字符串(这样也更简洁)。
PyCharm IDE 有许多编码配置可以连续细化,从全局到项目,再到文件路径。默认情况下,所有内容都应设置为 UTF-8,尤其是源代码。请参阅官方 PyCharm 文档 Configure file encoding settings。
如果您正在处理外部数据文件,则上述情况应该例外,在这种情况下,您的源代码仍应保留为 UTF-8,并且数据文件应以所需的任何编码打开。大多数关于 UnicodeDecodeError
的问题都是关于在使用 open()
function to open some data file 时指定正确的文件编码(它们与您编写代码的源文件的编码无关)。
当您的源文件导致此错误时,常见的原因是复制粘贴或打开未以 UTF-8 编码的源代码文件。 (复制粘贴特别出乎意料,当您从未以 UTF-8 编码的文件中复制并且 IDE 不会自动将您复制粘贴的内容转换为编辑器时)。这可能会导致上述错误。因此,您应该缩小编码不是 UTF-8 的源代码文件的范围并进行转换。
我们无法访问您的项目文件,但我收到的错误消息是调试器试图打开未以 UTF-8 编码的用户源代码文件,这与 IDE 配置和模块编码。
File "D:\Program Files\JetBrains\PyCharm Community Edition 2020.3.3\plugins\python-ce\helpers\pydev_pydevd_bundle\pydevd_comm.py"
我遇到了同样的问题,并在更改了解释器设置中使用的 python 可执行文件的大小写后最终修复了它,如 here 所述。长话短说,有时 PyCharm 尝试使用 Python
(“P”大写)而不是 python
在 venv
目录中执行符号链接。更改后我可以再次调试。
查看 Stefan Ukena 在此主题上的回答:
https://youtrack.jetbrains.com/issue/PY-14497#focus=Comments-27-5243196.0-0
引用以防 link 死亡:
You might need to change your Python Interpreter in the Pycharm > settings. In my case (using pipenv), it was pointing to /Library/.../bin/Python with an uppercase P. Opening the folder and checking, I found that the file or symlink was actually python with a lowercase p. Changing it from .../Python to .../python in the Pycharm settings fixed this problem. (I had to restart Pycharm afterwards.)
它对我也有帮助,但只有当我切换到 pipenv 而不是通常的 venv 时。我将 Python 更改为 python 并且调试器工作了,但我仍然得到 errors/warnings:
OSError: [Errno 9] Bad file descriptor
但它仍然有效。没有调试器,它按预期工作并且没有上述错误。
当前结论:
converted
文件的encoding
为utf-8
->utf-8 big
->ansi
->utf-8
。 每次转换后重新打开文件。
观察了一段时间,没有这个错误。
我用PyCharm调试.py
文件时,same
文件有时会出现UnicodeDecodeError,有时又正常。我的操作系统是Windows10,PyCharm版本是2020.3.3社区版。
错误如下:
Traceback (most recent call last):
File "D:\Program Files\JetBrains\PyCharm Community Edition 2020.3.3\plugins\python-ce\helpers\pydev\_pydevd_bundle\pydevd_comm.py", line 301, in _on_run
r = r.decode('utf-8')
UnicodeDecodeError: 'utf-8' codec can't decode bytes in position 1022-1023: unexpected end of data
我尝试在文件头添加如下代码,但有时还是报错,如何解决?
#!/usr/bin/env Python
# coding=utf-8
我找到了另一种使用记事本另存为 UTF-8 文档的方法。我试过了,但有时还是会报错。
添加'ignore'。成功了。
r = r.decode('utf-8', 'ignore')
问题中描述的问题没有一个单一的答案。许多问题都可能导致指示的错误,因此最好在 PyCharm IDE.
的上下文中解决几个可能的因素每个 Python 文件
.py
(或与此相关的任何其他文件)都有编码。.py
源代码文件的默认编码是 Unicode UTF-8。这个问题是初学者经常遇到的,所以我们从官方文档中找出相关引述(以减少不必要的阅读时间):The default encoding for Python source code is UTF-8, so you can simply include a Unicode character in a string literal.
这意味着在大多数情况下您不需要编码字符串,请参阅 Python Source Code Encodings - PEP 263。目前的做法是将源文件默认编码为 UTF-8 并省略模块顶部的编码字符串(这样也更简洁)。
PyCharm IDE 有许多编码配置可以连续细化,从全局到项目,再到文件路径。默认情况下,所有内容都应设置为 UTF-8,尤其是源代码。请参阅官方 PyCharm 文档 Configure file encoding settings。
如果您正在处理外部数据文件,则上述情况应该例外,在这种情况下,您的源代码仍应保留为 UTF-8,并且数据文件应以所需的任何编码打开。大多数关于
UnicodeDecodeError
的问题都是关于在使用open()
function to open some data file 时指定正确的文件编码(它们与您编写代码的源文件的编码无关)。当您的源文件导致此错误时,常见的原因是复制粘贴或打开未以 UTF-8 编码的源代码文件。 (复制粘贴特别出乎意料,当您从未以 UTF-8 编码的文件中复制并且 IDE 不会自动将您复制粘贴的内容转换为编辑器时)。这可能会导致上述错误。因此,您应该缩小编码不是 UTF-8 的源代码文件的范围并进行转换。
我们无法访问您的项目文件,但我收到的错误消息是调试器试图打开未以 UTF-8 编码的用户源代码文件,这与 IDE 配置和模块编码。
File "D:\Program Files\JetBrains\PyCharm Community Edition 2020.3.3\plugins\python-ce\helpers\pydev_pydevd_bundle\pydevd_comm.py"
我遇到了同样的问题,并在更改了解释器设置中使用的 python 可执行文件的大小写后最终修复了它,如 here 所述。长话短说,有时 PyCharm 尝试使用 Python
(“P”大写)而不是 python
在 venv
目录中执行符号链接。更改后我可以再次调试。
查看 Stefan Ukena 在此主题上的回答:
https://youtrack.jetbrains.com/issue/PY-14497#focus=Comments-27-5243196.0-0
引用以防 link 死亡:
You might need to change your Python Interpreter in the Pycharm > settings. In my case (using pipenv), it was pointing to /Library/.../bin/Python with an uppercase P. Opening the folder and checking, I found that the file or symlink was actually python with a lowercase p. Changing it from .../Python to .../python in the Pycharm settings fixed this problem. (I had to restart Pycharm afterwards.)
它对我也有帮助,但只有当我切换到 pipenv 而不是通常的 venv 时。我将 Python 更改为 python 并且调试器工作了,但我仍然得到 errors/warnings:
OSError: [Errno 9] Bad file descriptor
但它仍然有效。没有调试器,它按预期工作并且没有上述错误。