在 Alpine Linux 上安装 sklearn 时缺少 numpy header

Missing numpy header while installing sklearn on Alpine Linux

我正在尝试在 Docker 图像 (FROM astronomerinc/ap-airflow:master-1.10.5-onbuild) 上安装 sklearn。源镜像自带环境:

我的 requirements.txt 中有 scipy==1.3.1 并且使用 pip 安装它没有问题,但是当我将 scikit-learn 添加到 requirements.txt 并再次重建时,我得到了这个错误说缺少 numpy header:

    creating build/temp.linux-x86_64-3.7
    creating build/temp.linux-x86_64-3.7/sklearn
    creating build/temp.linux-x86_64-3.7/sklearn/svm
    creating build/temp.linux-x86_64-3.7/sklearn/svm/src
    creating build/temp.linux-x86_64-3.7/sklearn/svm/src/libsvm
    compile options: '-I/usr/lib/python3.7/site-packages/numpy/core/include -c'
    g++: sklearn/svm/src/libsvm/libsvm_template.cpp
    ar: adding 1 object files to build/temp.linux-x86_64-3.7/liblibsvm-skl.a
    running build_ext
    customize UnixCCompiler
    customize UnixCCompiler using build_ext
    resetting extension 'sklearn.svm.liblinear' language from 'c' to 'c++'.
    customize UnixCCompiler
    customize UnixCCompiler using build_ext
    building 'sklearn.__check_build._check_build' extension
    compiling C sources
    C compiler: gcc -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -Os -fomit-frame-pointer -g -Os -fomit-frame-pointer -g -Os -fomit-frame-pointer -g -DTHREAD_STACK_SIZE=0x100000 -fPIC

    creating build/temp.linux-x86_64-3.7/sklearn/__check_build
    compile options: '-I/usr/lib/python3.7/site-packages/numpy/core/include -I/usr/lib/python3.7/site-packages/numpy/core/include -I/usr/include/python3.7m -c'
    gcc: sklearn/__check_build/_check_build.c
    gcc -shared -Wl,--as-needed -Wl,--as-needed build/temp.linux-x86_64-3.7/sklearn/__check_build/_check_build.o -L/usr/lib -Lbuild/temp.linux-x86_64-3.7 -lpython3.7m -o build/lib.linux-x86_64-3.7/sklearn/__check_build/_check_build.cpython-37m-x86_64-linux-gnu.so
    building 'sklearn.cluster._dbscan_inner' extension
    compiling C++ sources
    C compiler: g++ -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -Os -fomit-frame-pointer -g -Os -fomit-frame-pointer -g -Os -fomit-frame-pointer -g -DTHREAD_STACK_SIZE=0x100000 -fPIC

    creating build/temp.linux-x86_64-3.7/sklearn/cluster
    compile options: '-I/usr/lib/python3.7/site-packages/numpy/core/include -I/usr/lib/python3.7/site-packages/numpy/core/include -I/usr/include/python3.7m -c'
    g++: sklearn/cluster/_dbscan_inner.cpp
    sklearn/cluster/_dbscan_inner.cpp:652:10: fatal error: numpy/arrayobject.h: No such file or directory
     #include "numpy/arrayobject.h"
              ^~~~~~~~~~~~~~~~~~~~~
    compilation terminated.
    error: Command "g++ -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -Os -fomit-frame-pointer -g -Os -fomit-frame-pointer -g -Os -fomit-frame-pointer -g -DTHREAD_STACK_SIZE=0x100000 -fPIC -I/usr/lib/python3.7/site-packages/numpy/core/include -I/usr/lib/python3.7/site-packages/numpy/core/include -I/usr/include/python3.7m -c sklearn/cluster/_dbscan_inner.cpp -o build/temp.linux-x86_64-3.7/sklearn/cluster/_dbscan_inner.o -MMD -MF build/temp.linux-x86_64-3.7/sklearn/cluster/_dbscan_inner.o.d" failed with exit status 1
    ----------------------------------------
ERROR: Command errored out with exit status 1: /usr/bin/python3.7 -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-o8ktwf40/scikit-learn/setup.py'"'"'; __file__='"'"'/tmp/pip-install-o8ktwf40/scikit-learn/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record /tmp/pip-record-p6ejlhi_/install-record.txt --single-version-externally-managed --compile Check the logs for full command output.
WARNING: You are using pip version 19.2.1, however version 19.2.3 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
The command '/bin/sh -c pip install --no-cache-dir -q -r requirements.txt' returned a non-zero code: 1


我尝试过的几件事:

None 不幸的是,他们中的一些人工作了。 This post 建议手动设置路径,但这不是我要找的答案。

见解?感谢您的帮助!

我建议您在 Dockerfile:

中安装 py-numpy-dev
 RUN apk add  py-numpy-dev