且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在 PyCharm 社区中调试 Python 文件时出现 UnicodeDecodeError: 'utf-8'

更新时间:2023-11-27 12:49:16

对于问题中所描述的问题,没有单一的答案.许多问题都可能导致指示的错误,因此***在 PyCharm IDE 的上下文中解决几个可能的因素.

  1. 每个 Python 文件 .py(或任何其他与此相关的文件)都有一个编码..py 源代码文件的默认编码是 Unicode UTF-8.这个问题是初学者经常遇到的,所以我们从官方文档中找出相关引用(以缩短不必要的阅读时间):

    Python 的 Unicode 支持>

    Python 源代码的默认编码是 UTF-8,因此您可以简单地在字符串文字中包含一个 Unicode 字符.

    这意味着在大多数情况下您不需要编码字符串,请参阅 Python 源代码代码编码 - PEP 263.目前的做法是将源文件默认编码为 UTF-8,并省略模块顶部的编码字符串(这也更简洁).

  2. PyCharm IDE 有许多编码配置,可以依次改进,从全局到项目,再到文件路径.默认情况下,所有内容都应设置为 UTF-8,尤其是源代码.请参阅 PyCharm 官方文档配置文件编码设置.

  3. 上述例外情况应该是如果您正在处理外部数据文件,在这种情况下,您的源代码仍应保留为 UTF-8,并且数据文件应以所需的任何编码打开.大多数关于 UnicodeDecodeError 的问题是关于在使用 open() 函数打开一些数据文件a>(它们与您编写代码的源文件的编码无关).

  4. 当您的源文件导致此错误时,一个常见的原因是在复制粘贴或打开未以 UTF-8 编码的源代码文件之后.(复制粘贴尤其出乎意料,当您从未以 UTF-8 编码的文件中复制时,IDE 不会自动将您复制粘贴的内容转换到编辑器中).这可能会导致上述错误.因此,您应该缩小哪个源代码文件的编码不是 UTF-8 并进行转换.

我们无权访问您的项目文件,但是当调试器尝试打开未以 UTF-8 编码的用户源代码文件时,向我显示的错误消息与 IDE 配置和模块编码相反.

文件D:\Program Files\JetBrains\PyCharm Community Edition 2020.3.3\plugins\python-ce\helpers\pydev_pydevd_bundle\pydevd_comm.py"

Current conclusion:

The encoding of the converted file is utf-8->utf-8 big->ansi -> utf-8. Reopen the file after each conversion.

After observing for a period of time, there is no such error.


When I use PyCharm to debug .py files, the same file sometimes has UnicodeDecodeError, sometimes it’s normal. My operating system is Windows 10, PyCharm version is 2020.3.3 Community edition.

The error is as follows:

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

I tried to add the following code to the header of the file, but sometimes I still get an error, how to solve it?

#!/usr/bin/env Python
# coding=utf-8

I found another way to save as a UTF-8 document with Notepad. I tried it, but there are still errors sometimes.

There isn't one single answer to the problem as it is described in the question. A number of issues can cause the indicated error, so it's best to address the several possible factors in the context of the PyCharm IDE.

  1. Every Python file .py (or any other file for that matter) has an encoding. The default encoding of a .py source code file is Unicode UTF-8. This problem is frequently faced by beginners, so lets pinpoint the relevant quotes from the official documentation (to shorten any unnecessary reading time):

    Python’s Unicode Support

    The default encoding for Python source code is UTF-8, so you can simply include a Unicode character in a string literal.

    This means in most circumstances you shouldn't need the encoding string, see Python Source Code Encodings - PEP 263. Current practice is having the source files encoded by default in UTF-8 and omitting the encoding string at the top of the module (this is also more concise).

  2. The PyCharm IDE has a number of encoding configurations that can successively be refined, going from global, to project, to file path. By default, everything should be set to UTF-8, especially the source code. See the official PyCharm documentation Configure file encoding settings.

  3. The exception to the above should be if you are processing external data files, in which case your source code should still be kept as UTF-8 and the data file opened with whatever encoding it requires. Most questions about the UnicodeDecodeError are about specifying the right file encoding when using the open() function to open some data file (they are not about the encoding of the source files where you are writing your code).

  4. When your source files cause this error, a frequent cause is after copy-pasting, or opening, a source code file that is not encoded in UTF-8. (The copy-paste is especially unexpected, when you copy from a file that isn't encoded in UTF-8 and the IDE doesn't automatically convert what you are copy-pasting into the editor). This can cause the said error. So you should narrow down which source code file has the encoding that isn't UTF-8 and convert it.

We don't have access to your project files, but the error message to me reads as the debugger trying to open a user source code file that isn't encoded in UTF-8, contrary to the IDE configurations and module encoding.

File "D:\Program Files\JetBrains\PyCharm Community Edition 2020.3.3\plugins\python-ce\helpers\pydev_pydevd_bundle\pydevd_comm.py"