python - Unicode解码错误: 'charmap' codec can't decode byte 0x81 in position 73776

标签 python windows encoding

这是一个 Django 项目。 在本地 virtualenv 中运行 pip install -r requirements.txt 时遇到错误。

Collecting https://github.com/jedestep/Zappa/archive/layer-support.zip (from -r requirements\base.txt (line 9))
Using cached https://github.com/jedestep/Zappa/archive/layer-support.zip
ERROR: Command errored out with exit status 1:
     command: 'c:\users\user~1\desktop\project\project\venv\scripts\python.exe' -c 'import sys, setuptools, tokenize
    ; sys.argv[0] = '"'"'C:\\Users\\USER~1\\AppData\\Local\\Temp\\pip-req-build-6htw2gh2\\setup.py'"'"'; __file__='"'"'C:\
    \Users\\USER~1\\AppData\\Local\\Temp\\pip-req-build-6htw2gh2\\setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(
    __file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' e
    gg_info --egg-base 'C:\Users\USER~1\AppData\Local\Temp\pip-req-build-6htw2gh2\pip-egg-info'
         cwd: C:\Users\USER~1\AppData\Local\Temp\pip-req-build-6htw2gh2\
    Complete output (7 lines):
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "C:\Users\USER~1\AppData\Local\Temp\pip-req-build-6htw2gh2\setup.py", line 8, in <module>
        long_description = readme_file.read()
      File "c:\users\user~1\desktop\project\project\venv\lib\encodings\cp1252.py", line 23, in decode
        return codecs.charmap_decode(input,self.errors,decoding_table)[0]
    UnicodeDecodeError: 'charmap' codec can't decode byte 0x81 in position 73776: character maps to <undefined>
    ----------------------------------------
ERROR: Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.

要求在 vagrant 环境中按预期安装,但我记得它也可以在我的机器上本地运行。不幸的是,我不确定导致此错误的更改是什么,它可能是拉动的结果。

版本: Python:3.6.5 Django:2.2 点:20.0.2

我已经运行了 pip install --upgrade setuptools 但它并没有改变错误。 当我尝试运行 tox 时,自动测试数据库下载会出现类似的错误。

如有任何想法,我们将不胜感激。 谢谢。

最佳答案

The requirements do install as expected in the vagrant environment, however I recall that it would also work locally on my machine. Unfortunately, I'm not sure what was the change that lead to this error, it may have been the result of a pull.

问题出在您尝试安装的包以及您的环境配置上。我猜这个包在某一时刻更新并损坏了,因为他们在他们的自述文件中添加了表情符号或其他东西:如果你稍微看一下错误消息,你会发现在将内容读入 long_description 时出现问题,检查 the package's setup.py您可以在开始附近找到问题:

with open('README.md') as readme_file:
    long_description = readme_file.read()

这里的问题是,当您打开 文件时没有指定模式,它将是“文本”,因此 python 会自动将字节解码为 str,这很酷,除了 编码 它用于此类解码的是系统的“默认”解码器(通过调用 locale.getpreferredencoding(False) 找到),这通常不是我们想要的。

你的 Vagrant 环境和开发人员的机器可能有一个 UTF8 默认编码(就像现在大多数 unix 系统一样),同时你的本地 Windows 框没有并且默认使用 CP1252,这会爆炸。可悲的是,我不是一个 Windows 用户(对于开发人员),所以我不知道如何更改“默认编码”,考虑到对 SO 的粗略搜索,这甚至可能是不可能的。

我建议:

  • 要求包作者修复他们的 setup.py 以在打开文件时明确指定编码
  • 尝试了解更多关于 getpreferredencoding 的内部决策,以及是否可以通过 Windows 中的环境覆盖 preferredencoding(显然可以/很容易覆盖标准流的编码但是chcp/set PYTHONIOENCODING 显然不对正常的open 做任何事情)
  • 在适用于 Linux 的 Windows 子系统上运行您的东西,它可能开箱即用

PS:我冒昧地更新了你帖子的标签,因为 django、pip 和 eggs 确实与实际问题无关

关于python - Unicode解码错误: 'charmap' codec can't decode byte 0x81 in position 73776,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60200727/

相关文章:

python - 为具有代表 'group' 的多条线的图表创建图例

python - 如果python程序的参数是目录,我如何读取每个文件?

Bonobo git 服务器的 Windows 客户端

c# - XML Unicode 反序列化

php - 为什么 html_entity_decode() 适用于第一个字符串,而不适用于第二个?

python - Plotly:如何根据条件为两条线之间的填充着色?

Python 请求超时不适合网站流式传输

java - 如何优雅地停止java进程?

c++ - 在 Windows 中使用 C++ 截取窗口截图的最佳方法是什么?

使用 FileWriter 进行 Java UTF-8 编码