在python2.7-alpine中的Docker中安装熊猫时,Docker构建失败

时间:2019-07-10 11:55:53

标签: pandas python-2.7 docker pip alpine

我正在Docker中运行Flask应用程序。我也在用熊猫。我正在使用python2.7-alpine图像。之前它可以正常工作,即我可以使用相同的配置来生成图像。

但是现在我无法按照该图像构建图像:

ERROR: Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-install-Sm9A7D/pandas/
The command '/bin/sh -c pip install -r /tmp/requirements.txt' returned a non-zero code: 1

我无法更改python版本。由于我的应用程序是基于python2构建的,因此早就可以预期了。

我的Dockerfile

FROM python:2.7-alpine

RUN echo "ipv6" >> /etc/modules; 
#    echo "http://dl-cdn.alpinelinux.org/alpine/latest-stable/main" > /etc/apk/repositories; \
#    echo "http://dl-cdn.alpinelinux.org/alpine/latest-stable/community" >> /etc/apk/repositories; \
#    echo "http://dl-2.alpinelinux.org/alpine/latest-stable/community" >> /etc/apk/repositories; \
#    echo "http://dl-3.alpinelinux.org/alpine/latest-stable/community" >> /etc/apk/repositories; \
#    echo "http://dl-4.alpinelinux.org/alpine/latest-stable/community" >> /etc/apk/repositories; \
#    echo "http://dl-5.alpinelinux.org/alpine/latest-stable/community" >> /etc/apk/repositories

RUN rm -rf /var/cache/apk/* && \
    rm -rf /tmp/* && \
    apk update && \
    apk add --update bash sudo


#================================================================
# add dependencies
#================================================================

RUN apk add --update --no-cache g++ gcc libffi-dev make gpgme p11-kit openssl-dev openssh

#================================================================
# pip and required modules install
#================================================================

### Upgrade pip to prevent errors
RUN pip install setuptools --upgrade
ADD requirements.txt /tmp/requirements.txt
RUN pip install -r /tmp/requirements.txt

完整的错误回溯是:

ERROR: Complete output from command python setup.py egg_info:
    ERROR: Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "/tmp/pip-install-Sm9A7D/pandas/setup.py", line 749, in <module>
        **setuptools_kwargs)
      File "/usr/local/lib/python2.7/site-packages/setuptools/__init__.py", line 144, in setup
        _install_setup_requires(attrs)
      File "/usr/local/lib/python2.7/site-packages/setuptools/__init__.py", line 139, in _install_setup_requires
        dist.fetch_build_eggs(dist.setup_requires)
      File "/usr/local/lib/python2.7/site-packages/setuptools/dist.py", line 717, in fetch_build_eggs
        replace_conflicting=True,
      File "/usr/local/lib/python2.7/site-packages/pkg_resources/__init__.py", line 782, in resolve
        replace_conflicting=replace_conflicting
      File "/usr/local/lib/python2.7/site-packages/pkg_resources/__init__.py", line 1065, in best_match
        return self.obtain(req, installer)
      File "/usr/local/lib/python2.7/site-packages/pkg_resources/__init__.py", line 1077, in obtain
        return installer(requirement)
      File "/usr/local/lib/python2.7/site-packages/setuptools/dist.py", line 784, in fetch_build_egg
        return cmd.easy_install(req)
      File "/usr/local/lib/python2.7/site-packages/setuptools/command/easy_install.py", line 679, in easy_install
        return self.install_item(spec, dist.location, tmpdir, deps)
      File "/usr/local/lib/python2.7/site-packages/setuptools/command/easy_install.py", line 705, in install_item
        dists = self.install_eggs(spec, download, tmpdir)
      File "/usr/local/lib/python2.7/site-packages/setuptools/command/easy_install.py", line 890, in install_eggs
        return self.build_and_install(setup_script, setup_base)
      File "/usr/local/lib/python2.7/site-packages/setuptools/command/easy_install.py", line 1158, in build_and_install
        self.run_setup(setup_script, setup_base, args)
      File "/usr/local/lib/python2.7/site-packages/setuptools/command/easy_install.py", line 1144, in run_setup
        run_setup(setup_script, args)
      File "/usr/local/lib/python2.7/site-packages/setuptools/sandbox.py", line 253, in run_setup
        raise
      File "/usr/local/lib/python2.7/contextlib.py", line 35, in __exit__
        self.gen.throw(type, value, traceback)
      File "/usr/local/lib/python2.7/site-packages/setuptools/sandbox.py", line 195, in setup_context
        yield
      File "/usr/local/lib/python2.7/contextlib.py", line 35, in __exit__
        self.gen.throw(type, value, traceback)
      File "/usr/local/lib/python2.7/site-packages/setuptools/sandbox.py", line 166, in save_modules
        saved_exc.resume()
      File "/usr/local/lib/python2.7/site-packages/setuptools/sandbox.py", line 141, in resume
        six.reraise(type, exc, self._tb)
      File "/usr/local/lib/python2.7/site-packages/setuptools/sandbox.py", line 154, in save_modules
        yield saved
      File "/usr/local/lib/python2.7/site-packages/setuptools/sandbox.py", line 195, in setup_context
        yield
      File "/usr/local/lib/python2.7/site-packages/setuptools/sandbox.py", line 250, in run_setup
        _execfile(setup_script, ns)
      File "/usr/local/lib/python2.7/site-packages/setuptools/sandbox.py", line 45, in _execfile
        exec(code, globals, locals)
      File "/tmp/easy_install-2BV0tR/numpy-1.17.0rc1/setup.py", line 31, in <module>
        def is_platform_mac():
    RuntimeError: Python version >= 3.5 required.
    ----------------------------------------
ERROR: Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-install-Sm9A7D/pandas/
The command '/bin/sh -c pip install -r /tmp/requirements.txt' returned a non-zero code: 1

1 个答案:

答案 0 :(得分:2)

错误告诉您原因:

  


中的文件“ /tmp/easy_install-r9No9Q/numpy-1.17.0rc1/setup.py”,第31行         def is_platform_mac():
     RuntimeError:Python版本> = 3.5必需。

从熊猫0.23.4 source:我们可以看到numpy的最低版本是1.9.0

  

min_numpy_ver ='1.9.0'

因此,如果您不是自己安装版本,pandas只会安装numpy的最新版本,该版本>1.9.0为您服务,这里是numpy-1.17.0rc1,但需要>python3.5here所示,因此会发生故障。

  

python_requires ='> = 3.5'

最后,如果检查numpy的源代码,您会发现1.16.4是支持python2.7的最新版本,请参见this

python_requires='>=2.7,!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*',

因此,最终解决方案

预安装numpy 1.16.4,然后熊猫发现那里有一个合适的numpy,并且不会为您安装最新的numpy。结果,将不会发生错误,详细步骤如下:

apk update
apk add build-base
pip install numpy==1.16.4
pip install pandas==0.23.4

对于Dockerfile,将上述命令添加到一个RUN即可解决:

RUN apk update && \
    apk add build-base && \
    pip install numpy==1.16.4 && \
    pip install pandas==0.23.4

另外,您说的是

  

之前效果很好,也就是说,我能够使用相同的配置来构建图像。

这是因为numpy latest version刚于2019年7月1日更新,在此之前,它始终是1.16.4版本,没有python27的问题,但是现在,情况发生了变化...