在 docker 镜像 python:3.9.13-alpine3.16 上使用“pip install psutil”错误:找不到 linux/ethtool.h

Guf*_*Guf 7 python makefile psutil alpine-linux

我尝试在 docker 中安装 python 模块 psutil python:3.9.13-alpine3.16
\n但它报告以下错误:

\n
Building wheels for collected packages: psutil\n  Building wheel for psutil (pyproject.toml) ... error\n  error: subprocess-exited-with-error\n  \n  \xc3\x97 Building wheel for psutil (pyproject.toml) did not run successfully.\n  \xe2\x94\x82 exit code: 1\n  \xe2\x95\xb0\xe2\x94\x80> [51 lines of output]\n      /tmp/tmpb62wij4i.c:1:10: fatal error: linux/ethtool.h: No such file or directory\n          1 | #include <linux/ethtool.h>\n            |          ^~~~~~~~~~~~~~~~~\n      compilation terminated.\n      running bdist_wheel\n      running build\n      running build_py\n      creating build\n      creating build/lib.linux-x86_64-cpython-39\n      creating build/lib.linux-x86_64-cpython-39/psutil\n      copying psutil/__init__.py -> build/lib.linux-x86_64-cpython-39/psutil\n      copying psutil/_common.py -> build/lib.linux-x86_64-cpython-39/psutil\n      copying psutil/_compat.py -> build/lib.linux-x86_64-cpython-39/psutil\n      copying psutil/_psaix.py -> build/lib.linux-x86_64-cpython-39/psutil\n      copying psutil/_psbsd.py -> build/lib.linux-x86_64-cpython-39/psutil\n      copying psutil/_pslinux.py -> build/lib.linux-x86_64-cpython-39/psutil\n      copying psutil/_psosx.py -> build/lib.linux-x86_64-cpython-39/psutil\n      copying psutil/_psposix.py -> build/lib.linux-x86_64-cpython-39/psutil\n      copying psutil/_pssunos.py -> build/lib.linux-x86_64-cpython-39/psutil\n      copying psutil/_pswindows.py -> build/lib.linux-x86_64-cpython-39/psutil\n      creating build/lib.linux-x86_64-cpython-39/psutil/tests\n      copying psutil/tests/__init__.py -> build/lib.linux-x86_64-cpython-39/psutil/tests\n      copying psutil/tests/__main__.py -> build/lib.linux-x86_64-cpython-39/psutil/tests\n      copying psutil/tests/foo.py -> build/lib.linux-x86_64-cpython-39/psutil/tests\n      copying psutil/tests/runner.py -> build/lib.linux-x86_64-cpython-39/psutil/tests\n      copying psutil/tests/test_aix.py -> build/lib.linux-x86_64-cpython-39/psutil/tests\n      copying psutil/tests/test_bsd.py -> build/lib.linux-x86_64-cpython-39/psutil/tests\n      copying psutil/tests/test_connections.py -> build/lib.linux-x86_64-cpython-39/psutil/tests\n      copying psutil/tests/test_contracts.py -> build/lib.linux-x86_64-cpython-39/psutil/tests\n      copying psutil/tests/test_linux.py -> build/lib.linux-x86_64-cpython-39/psutil/tests\n      copying psutil/tests/test_memleaks.py -> build/lib.linux-x86_64-cpython-39/psutil/tests\n      copying psutil/tests/test_misc.py -> build/lib.linux-x86_64-cpython-39/psutil/tests\n      copying psutil/tests/test_osx.py -> build/lib.linux-x86_64-cpython-39/psutil/tests\n      copying psutil/tests/test_posix.py -> build/lib.linux-x86_64-cpython-39/psutil/tests\n      copying psutil/tests/test_process.py -> build/lib.linux-x86_64-cpython-39/psutil/tests\n      copying psutil/tests/test_sunos.py -> build/lib.linux-x86_64-cpython-39/psutil/tests\n      copying psutil/tests/test_system.py -> build/lib.linux-x86_64-cpython-39/psutil/tests\n      copying psutil/tests/test_testutils.py -> build/lib.linux-x86_64-cpython-39/psutil/tests\n      copying psutil/tests/test_unicode.py -> build/lib.linux-x86_64-cpython-39/psutil/tests\n      copying psutil/tests/test_windows.py -> build/lib.linux-x86_64-cpython-39/psutil/tests\n      running build_ext\n      building \'psutil._psutil_linux\' extension\n      creating build/temp.linux-x86_64-cpython-39\n      creating build/temp.linux-x86_64-cpython-39/psutil\n      gcc -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -DTHREAD_STACK_SIZE=0x100000 -fPIC -DPSUTIL_POSIX=1 -DPSUTIL_SIZEOF_PID_T=4 -DPSUTIL_VERSION=591 -DPSUTIL_LINUX=1 -DPSUTIL_ETHTOOL_MISSING_TYPES=1 -I/usr/local/include/python3.9 -c psutil/_psutil_common.c -o build/temp.linux-x86_64-cpython-39/psutil/_psutil_common.o\n      gcc -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -DTHREAD_STACK_SIZE=0x100000 -fPIC -DPSUTIL_POSIX=1 -DPSUTIL_SIZEOF_PID_T=4 -DPSUTIL_VERSION=591 -DPSUTIL_LINUX=1 -DPSUTIL_ETHTOOL_MISSING_TYPES=1 -I/usr/local/include/python3.9 -c psutil/_psutil_linux.c -o build/temp.linux-x86_64-cpython-39/psutil/_psutil_linux.o\n      psutil/_psutil_linux.c:19:10: fatal error: linux/version.h: No such file or directory\n         19 | #include <linux/version.h>\n            |          ^~~~~~~~~~~~~~~~~\n      compilation terminated.\n      error: command \'/usr/bin/gcc\' failed with exit code 1\n      [end of output]\n  \n  note: This error originates from a subprocess, and is likely not a problem with pip.\n  ERROR: Failed building wheel for psutil\nFailed to build psutil\nERROR: Could not build wheels for psutil, which is required to install pyproject.toml-based projects\n\n
Run Code Online (Sandbox Code Playgroud)\n

复发过程:

\n
    \n
  1. docker拉python:3.9.13-alpine3.16
  2. \n
  3. docker run --name alpine-python3 -it [image-id] /bin/sh
  4. \n
  5. (在容器中)# apk 添加构建基础
  6. \n
  7. (在容器中)# pip install psutil
  8. \n
\n

关键错误:

\n
Building wheels for collected packages: psutil\n  Building wheel for psutil (pyproject.toml) ... error\n  error: subprocess-exited-with-error\n  \n  \xc3\x97 Building wheel for psutil (pyproject.toml) did not run successfully.\n  \xe2\x94\x82 exit code: 1\n  \xe2\x95\xb0\xe2\x94\x80> [51 lines of output]\n      /tmp/tmpb62wij4i.c:1:10: fatal error: linux/ethtool.h: No such file or directory\n          1 | #include <linux/ethtool.h>\n            |          ^~~~~~~~~~~~~~~~~\n      compilation terminated.\n      running bdist_wheel\n      running build\n      running build_py\n      creating build\n      creating build/lib.linux-x86_64-cpython-39\n      creating build/lib.linux-x86_64-cpython-39/psutil\n      copying psutil/__init__.py -> build/lib.linux-x86_64-cpython-39/psutil\n      copying psutil/_common.py -> build/lib.linux-x86_64-cpython-39/psutil\n      copying psutil/_compat.py -> build/lib.linux-x86_64-cpython-39/psutil\n      copying psutil/_psaix.py -> build/lib.linux-x86_64-cpython-39/psutil\n      copying psutil/_psbsd.py -> build/lib.linux-x86_64-cpython-39/psutil\n      copying psutil/_pslinux.py -> build/lib.linux-x86_64-cpython-39/psutil\n      copying psutil/_psosx.py -> build/lib.linux-x86_64-cpython-39/psutil\n      copying psutil/_psposix.py -> build/lib.linux-x86_64-cpython-39/psutil\n      copying psutil/_pssunos.py -> build/lib.linux-x86_64-cpython-39/psutil\n      copying psutil/_pswindows.py -> build/lib.linux-x86_64-cpython-39/psutil\n      creating build/lib.linux-x86_64-cpython-39/psutil/tests\n      copying psutil/tests/__init__.py -> build/lib.linux-x86_64-cpython-39/psutil/tests\n      copying psutil/tests/__main__.py -> build/lib.linux-x86_64-cpython-39/psutil/tests\n      copying psutil/tests/foo.py -> build/lib.linux-x86_64-cpython-39/psutil/tests\n      copying psutil/tests/runner.py -> build/lib.linux-x86_64-cpython-39/psutil/tests\n      copying psutil/tests/test_aix.py -> build/lib.linux-x86_64-cpython-39/psutil/tests\n      copying psutil/tests/test_bsd.py -> build/lib.linux-x86_64-cpython-39/psutil/tests\n      copying psutil/tests/test_connections.py -> build/lib.linux-x86_64-cpython-39/psutil/tests\n      copying psutil/tests/test_contracts.py -> build/lib.linux-x86_64-cpython-39/psutil/tests\n      copying psutil/tests/test_linux.py -> build/lib.linux-x86_64-cpython-39/psutil/tests\n      copying psutil/tests/test_memleaks.py -> build/lib.linux-x86_64-cpython-39/psutil/tests\n      copying psutil/tests/test_misc.py -> build/lib.linux-x86_64-cpython-39/psutil/tests\n      copying psutil/tests/test_osx.py -> build/lib.linux-x86_64-cpython-39/psutil/tests\n      copying psutil/tests/test_posix.py -> build/lib.linux-x86_64-cpython-39/psutil/tests\n      copying psutil/tests/test_process.py -> build/lib.linux-x86_64-cpython-39/psutil/tests\n      copying psutil/tests/test_sunos.py -> build/lib.linux-x86_64-cpython-39/psutil/tests\n      copying psutil/tests/test_system.py -> build/lib.linux-x86_64-cpython-39/psutil/tests\n      copying psutil/tests/test_testutils.py -> build/lib.linux-x86_64-cpython-39/psutil/tests\n      copying psutil/tests/test_unicode.py -> build/lib.linux-x86_64-cpython-39/psutil/tests\n      copying psutil/tests/test_windows.py -> build/lib.linux-x86_64-cpython-39/psutil/tests\n      running build_ext\n      building \'psutil._psutil_linux\' extension\n      creating build/temp.linux-x86_64-cpython-39\n      creating build/temp.linux-x86_64-cpython-39/psutil\n      gcc -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -DTHREAD_STACK_SIZE=0x100000 -fPIC -DPSUTIL_POSIX=1 -DPSUTIL_SIZEOF_PID_T=4 -DPSUTIL_VERSION=591 -DPSUTIL_LINUX=1 -DPSUTIL_ETHTOOL_MISSING_TYPES=1 -I/usr/local/include/python3.9 -c psutil/_psutil_common.c -o build/temp.linux-x86_64-cpython-39/psutil/_psutil_common.o\n      gcc -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -DTHREAD_STACK_SIZE=0x100000 -fPIC -DPSUTIL_POSIX=1 -DPSUTIL_SIZEOF_PID_T=4 -DPSUTIL_VERSION=591 -DPSUTIL_LINUX=1 -DPSUTIL_ETHTOOL_MISSING_TYPES=1 -I/usr/local/include/python3.9 -c psutil/_psutil_linux.c -o build/temp.linux-x86_64-cpython-39/psutil/_psutil_linux.o\n      psutil/_psutil_linux.c:19:10: fatal error: linux/version.h: No such file or directory\n         19 | #include <linux/version.h>\n            |          ^~~~~~~~~~~~~~~~~\n      compilation terminated.\n      error: command \'/usr/bin/gcc\' failed with exit code 1\n      [end of output]\n  \n  note: This error originates from a subprocess, and is likely not a problem with pip.\n  ERROR: Failed building wheel for psutil\nFailed to build psutil\nERROR: Could not build wheels for psutil, which is required to install pyproject.toml-based projects\n\n
Run Code Online (Sandbox Code Playgroud)\n

那么,我需要提前预安装什么来修复呢?\n谢谢!

\n

Mic*_*uth 17

您需要添加 linux-headers 包。

apk add build-base linux-headers
python -m pip install psutil
Run Code Online (Sandbox Code Playgroud)

更新0

太好了,您需要 linux-headers 包。但是,为什么需要 linux-headers 包?

该包提供了内核源代码中的数据结构和函数签名。为了编译调用这些函数的模块,需要此信息。我们不需要实际的源代码,只需要规范、接口(如果您愿意的话)。psutil例如,这是使用主机操作系统的网络实用程序所需的信息。

为什么要将其与实现分开?如今,对于个人计算机来说,这基本上是不必要的。但是,当存储空间小得多且昂贵得多时,这非常有用。当您所做的只是构建一个调用ethtool.

一般来说,每当您构建与 Linux 操作系统交互的模块时,您都需要安装 linux-headers。