在构建Docker容器时,Python3出现神秘的UnicodeDecodeError错误

5
我是一名有用的助手,会翻译文字。
我正在创建一个Python 3应用程序。 https://github.com/Omrigan/TED-analysis 为了部署,我想使用位于我的Github存储库根目录中的Dockerfile进行Docker化(您可以检查它)。因此,当我执行“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)

那么,我该怎么办呢?

1
你的Dockerfile应该处理语言环境,参考http://jaredmarkell.com/docker-and-locales/,并且最好使用一些UTF-8变体。 - user2915097
2个回答

1
我有同样的问题。 原因是我选择的语言环境(即en_US.utf8)未安装。 安装此语言环境解决了我的问题。
设置语言环境的方法:
locale-gen en_US.utf8
dpkg-reconfigure locales

选择en_US.utf8作为您的默认区域设置


1
似乎httpretty在定位其版本号时会做一些奇怪的事情——它打开其中一个包含非ASCII字符的源文件,而没有声明编码。在Python 3中,这将使用您的区域设置,而在您的情况下,似乎已经损坏或设置为LANG=C|POSIX。您有以下选项:
  1. Download httpretty-0.8.10, edit httpretty/__init__.py and remove the non-ascii chars (ã).
  2. Set your locale to en_US.UTF-8
  3. I see that httpretty 0.8.14 has references to being Python 3 compliant. Try installing with:

    pip3 install httpretty==0.8.14
    

1
似乎是httpretty中的一个bug:Python源文件的编码与区域设置无关,因此默认使用区域设置编码的内置open()函数是读取Python源代码的错误方法。 Python 3文件要么是utf-8格式,要么存在显式的编码声明(例如# -*- coding: utf-8 -*-)。可以使用tokenize.open()函数将源代码解码为Unicode。 - jfs

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接