【问题标题】:Mysterious UnicodeDecodeError in python3 while building Docker container构建Docker容器时python3中的神秘UnicodeDecodeError
【发布时间】:2016-07-23 18:41:27
【问题描述】:

我正在创建一个 python 3 应用程序。 https://github.com/Omrigan/TED-analysis

为了部署,我想使用 Docker 和位于我的 Github repo 根目录中的 Dockerfile(你可以检查一下)。因此,当我执行“docker build”时。我在这一行得到一个错误:

RUN pip3 install --upgrade  -r /root/ted_talks/requirements.txt

来自控制台的日志:

  Collecting httpretty==0.8.10 (from smart-open>=1.2.1->gensim->-r /root/ted_talks/requirements.txt (line 4))
  Downloading httpretty-0.8.10.tar.gz (41kB)
    Complete output from command python setup.py egg_info:
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "/tmp/pip-build-em459e9u/httpretty/setup.py", line 86, in <module>
        version=read_version(),
      File "/tmp/pip-build-em459e9u/httpretty/setup.py", line 46, in read_version
        finder.visit(ast.parse(local_file('httpretty', '__init__.py')))
      File "/tmp/pip-build-em459e9u/httpretty/setup.py", line 78, in <lambda>
        open(os.path.join(os.path.dirname(__file__), *f)).read()
      File "/usr/lib/python3.4/encodings/ascii.py", line 26, in decode
        return codecs.ascii_decode(input, self.errors)[0]
    UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 133: ordinal not in range(128)

那么,我该怎么办?

【问题讨论】:

标签: python python-3.x unicode docker


【解决方案1】:

似乎 httpretty 做了一些有趣的事情来定位它的版本号 - 它打开一个源文件,其中包含非 ascii 字符,而不声明编码。在 Python 3 中,这将使用您的语言环境,在您的情况下,该语言环境似乎已损坏或设置为 LANG=C|POSIX

您有以下选择:

  1. 下载 httpretty-0.8.10,编辑 httpretty/__init__.py 并删除非 ascii 字符 (ã)。
  2. 将您的语言环境设置为en_US.UTF-8
  3. 我看到 httpretty 0.8.14 提到了与 Python 3 兼容。尝试安装:

    pip3 install httpretty==0.8.14
    

【讨论】:

  • 这似乎是httpretty中的一个错误:Python源文件的编码与语言环境无关,因此默认使用语言环境编码的内置open()是错误的方法阅读 Python 源代码。 Python 3 文件要么是 utf-8,要么存在显式编码声明(例如 # -*- coding: utf-8 -*-)。可以使用tokenize.open(),将源代码解码为Unicode。
【解决方案2】:

我遇到了同样的问题。 原因是我选择的语言环境(即 en_US.utf8)没有安装。 安装这个语言环境解决了我的问题。

设置语言环境:

locale-gen en_US.utf8
dpkg-reconfigure locales

并选择 en_US.utf8 作为您的默认语言环境

【讨论】:

    猜你喜欢
    • 2015-10-19
    • 2012-12-08
    • 1970-01-01
    • 1970-01-01
    • 2020-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多