部署至BalenaOS树莓派CM4时netifaces编译失败求助
在将代码部署到运行BalenaOS的树莓派CM4集线器时,安装netifaces库构建失败,错误信息如下:
Building wheel for netifaces (setup.py): started [main] Building wheel for netifaces (setup.py): finished with status 'error' [main] error: subprocess-exited-with-error [main] × python setup.py bdist_wheel did not run successfully. [main] │ exit code: 1 [main] ╰─> [37 lines of output] [main] /home/gateway-hub/.local/lib/python3.9/site-packages/setuptools/config/setupcfg.py:293: _DeprecatedConfig: Deprecated config in `setup.cfg` [main] !! [main] ******************************************************************************** [main] The license_file parameter is deprecated, use license_files instead. [main] By 2023-Oct-30, you need to update your project and remove deprecated calls [main] or your builds will no longer be supported. [main] ******************************************************************************** [main] !! [main] parsed = self.parsers.get(option_name, lambda x: x)(value) [main] running bdist_wheel [main] running build [main] running build_ext [main] checking for getifaddrs...not found. [main] checking for getnameinfo...not found. [main] checking for socket IOCTLs...not found. [main] checking for optional header files...none found. [main] checking whether struct sockaddr has a length field...no. [main] checking which sockaddr_xxx structs are defined...none! [main] checking for routing socket support...no. [main] checking for sysctl(CTL_NET...) support...no. checking for netlink support...no. building 'netifaces' extension gcc -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -fPIC -DNETIFACES_VERSION=0.11.0 -I/usr/local/include/python3.9 -c netifaces.c -o build/temp.linux-aarch64-cpython-39/netifaces.o In file included from /usr/lib/gcc/aarch64-linux-gnu/12/include/syslimits.h:7, [main] from /usr/lib/gcc/aarch64-linux-gnu/12/include/limits.h:34, [main] from /usr/local/include/python3.9/Python.h:11, [main] from netifaces.c:1: [main] /usr/lib/gcc/aarch64-linux-gnu/12/include/limits.h:203:15: fatal error: limits.h: No such file or directory [main] 203 | #include_next <limits.h> /* recurse down to the real one */ [main] | ^~~~~~~~~~ [main] compilation terminated. [main] error: command '/usr/bin/gcc' failed with exit code 1 [main] [end of output] [main] note: This error originates from a subprocess, and is likely not a problem with pip. [main] [main] ERROR: Failed building wheel for netifaces [main] [main] Running setup.py clean for netifaces [main] Successfully built pykafka [main] Failed to build netifaces [main] ERROR: Could not build wheels for netifaces, which is required to install pyproject.toml-based projects
使用的Dockerfile:
FROM python:3.9-slim RUN apt-get update \ && apt-get install -y --no-install-recommends gcc \ && apt-get install --reinstall libgcc-12-dev RUN groupadd gateway-hub &&\ useradd gateway-hub -m -g gateway-hub USER gateway-hub WORKDIR /app COPY --chown=gateway-hub:gateway-hub . . RUN cd /app CMD ["python3", "-m", "venv", "."] RUN chmod +x /app/environ.sh RUN /app/environ.sh
配置虚拟环境和安装依赖的environ.sh脚本:
#!/bin/bash # Check if virtual environment exists if [ ! -d "/home/gateway-hub/hub/bin" ]; then # Create the virtual environment python -m venv "/home/gateway-hub/hub" fi # Activate the Python virtual environment source "/home/gateway-hub/hub/bin/activate" # Update python dependencies if needed if [ requirements_changed ]; then python3 -m pip install --upgrade setuptools pip pip install -r "/app/requirements.txt" fi
已尝试的修复方法:
- 重新安装
libgcc-12-dev - 更新
pip、setuptools - 单独使用
--use-pep517标志安装netifaces - 单独使用
--no-binary :all:和/或--no-cache-dir标志安装netifaces - 单独安装
wheel库
所有尝试均出现相同错误,询问是否为缺少依赖/包更新问题,或与硬件有关。
解决方案
1. 修复核心编译依赖缺失问题
错误中fatal error: limits.h: No such file or directory是关键,limits.h属于GNU C标准库的开发包,而python:3.9-slim镜像默认未安装该包。需要在Dockerfile的apt-get install命令中添加libc6-dev:
修改后的Dockerfile依赖安装部分:
RUN apt-get update \ && apt-get install -y --no-install-recommends gcc libc6-dev \ && apt-get clean && rm -rf /var/lib/apt/lists/*
添加libc6-dev后无需单独重装libgcc-12-dev,因为前者已包含必要的编译头文件。
2. 修复Dockerfile中的无效指令
Dockerfile中的CMD ["python3", "-m", "venv", "."]是容器启动时执行的命令,构建阶段不会运行,属于无效指令,建议直接删除。
3. 修复environ.sh中的无效条件判断
脚本中if [ requirements_changed ];的条件永远为真(requirements_changed被当作非空字符串处理),建议修改为基于文件修改时间的判断,确保仅在requirements.txt更新时重新安装依赖:
修改后的environ.sh条件部分:
# Update python dependencies if requirements.txt has changed if [ "/app/requirements.txt" -nt "/home/gateway-hub/hub/bin/activate" ]; then python3 -m pip install --upgrade setuptools pip wheel pip install -r "/app/requirements.txt" fi
同时将wheel加入升级命令,确保编译环境完整。
4. 硬件兼容性验证
树莓派CM4是aarch64架构,netifaces原生支持该架构,因此硬件本身不是问题,核心还是编译依赖缺失。
内容的提问来源于stack exchange,提问作者masonkimbell

