如何在Yocto中通过python3-pip-native安装Python构建依赖
问题
需要构建一个依赖大量Python包的二进制文件,之前手动在构建机用pip安装所有包成功,但流程繁琐,想改用原生工具python3-pip-native管理,无需用户手动操作。但在构建系统的do_configure()阶段执行命令:
python3 -m pip install -r ${S}/scripts/constraints.txt
时出现以下错误:
Defaulting to user installation because normal site-packages is not writeable
WARNING: Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'NewConnectionError('<pip._vendor.urllib3.connection.HTTPSConnection object at 0x7f26123a4a60>: Failed to establish a new connection: [Errno -3] Temporary failure in name resolution')': /simple/anytree/
WARNING: Retrying (Retry(total=3, connect=None, read=None, redirect=None, status=None)) after connection broken by 'NewConnectionError('<pip._vendor.urllib3.connection.HTTPSConnection object at 0x7f26123a4d90>: Failed to establish a new connection: [Errno -3] Temporary failure in name resolution')': /simple/anytree/
WARNING: Retrying (Retry(total=2, connect=None, read=None, redirect=None, status=None)) after connection broken by 'NewConnectionError('<pip._vendor.urllib3.connection.HTTPSConnection object at 0x7f26123a4f40>: Failed to establish a new connection: [Errno -3] Temporary failure in name resolution')': /simple/anytree/
WARNING: Retrying (Retry(total=1, connect=None, read=None, redirect=None, status=None)) after connection broken by 'NewConnectionError('<pip._vendor.urllib3.connection.HTTPSConnection object at 0x7f26123a50f0>: Failed to establish a new connection: [Errno -3] Temporary failure in name resolution')': /simple/anytree/
WARNING: Retrying (Retry(total=0, connect=None, read=None, redirect=None, status=None)) after connection broken by 'NewConnectionError('<pip._vendor.urllib3.connection.HTTPSConnection object at 0x7f26123a52a0>: Failed to establish a new connection: [Errno -3] Temporary failure in name resolution')': /simple/anytree/
ERROR: Could not find a version that satisfies the requirement anytree2.8.0 (from versions: none)
ERROR: No matching distribution found for anytree2.8.0
WARNING: exit code 1 from a shell command.
注:所有Python包仅用于构建,无需运行时依赖。
解决方案
1. 解决构建环境网络问题
构建系统(如Yocto/OpenEmbedded)默认构建隔离环境可能无网络访问权限,导致pip无法连接PyPI:
- 在构建配置
local.conf中添加BB_NO_NETWORK = "0",开启构建阶段网络访问(完成后可关闭以保证构建可重复性)。 - 验证构建机网络正常,在终端执行
ping pypi.org确认DNS解析和网络连通性。
2. 修正pip安装路径权限
错误提示"normal site-packages is not writeable",需指定具有写入权限的目录:
修改do_configure()中的命令为:
python3 -m pip install --prefix=${STAGING_DIR_HOST} -r ${S}/scripts/constraints.txt
或指定用户目录安装:
python3 -m pip install --user -r ${S}/scripts/constraints.txt
3. 采用离线预下载包构建(推荐)
构建系统优先推荐离线构建以避免网络依赖,保证可重复性:
- 提前下载所有依赖包到本地
files/python-packages目录:pip download -r constraints.txt -d ${S}/files/python-packages - 在
do_configure()中指定本地源安装:python3 -m pip install --no-index --find-links=${S}/files/python-packages -r ${S}/scripts/constraints.txt - 在
.bb文件中添加文件依赖:SRC_URI += "file://python-packages/"
4. 适配构建系统原生Python依赖管理
如果是Yocto/OpenEmbedded环境,更推荐通过配方管理构建依赖:
- 查找或编写目标Python包的原生配方(如
python3-anytree_2.8.0.bb)。 - 在项目
.bb文件中添加构建依赖:DEPENDS += "python3-anytree-native"
构建系统会自动处理依赖安装,无需手动调用pip,更符合原生工具链规范。
内容的提问来源于stack exchange,提问作者malyjak

