Docker build failing while installing pandas in docker in python2.7-alpine

Question:

I am running a flask application in Docker. I am also using Pandas. I am using python2.7-alpine image. Earlier it was working fine i.e. I was able to build images with the same configuration.

But now I am unable to build the image as it says:

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

I cannot change the python version. As my application is built on python2 and was as expected for a long back.

My 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

The full error track back is:

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
Asked By: KdPisda

||

Answers:

The error tells you the reason:

File “/tmp/easy_install-r9No9Q/numpy-1.17.0rc1/setup.py”, line 31, in
def is_platform_mac():
RuntimeError: Python version >= 3.5 required.

From pandas 0.23.4 source: we can see the minimum version for numpy is 1.9.0:

min_numpy_ver = ‘1.9.0’

So, if you did not install a version by yourself, pandas will just install a newest suitable version of numpy which >1.9.0 for you, here is numpy-1.17.0rc1, but it needs >python3.5 as seen here, so failure happens.

python_requires=’>=3.5′

Finally, if you check the source code of numpy, you will find 1.16.4 is the last version which support python2.7, see this,

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

So, final solution:

Pre-install numpy 1.16.4, then pandas find there is a suitable numpy there, and will not install a newest numpy for you. As a result, no error will happen, detail steps as next:

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

For Dockerfile, add above command to one RUN could make the fix:

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

Additional, you said in question:

Earlier it was working fine i.e. I was able to build images with the same configuration.

This is because numpy latest version was just updated July.1st 2019, before that, it always 1.16.4 version which don’t have issue for python27, but now, things changed…

Answered By: atline