python-yocto-env-setup

Python 环境配置(Yocto 兼容性)

问题

Yocto 项目 (i.MX BSP, Hardknott) 需要 Python ≤ 3.8,但系统是 Python 3.10:

ImportError: cannot import name 'Mapping' from 'collections'

原因:Python 3.10 将 collections.Mapping 移到了 collections.abc.Mapping,而旧版 bitbake 仍从 collections 导入。


方案:pyenv 安装 Python 3.8

1. 安装 pyenv

curl https://pyenv.run | bash

2. 配置 shell 环境

添加到 ~/.bashrc

echo 'export PATH="$HOME/.pyenv/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(pyenv init -)"' >> ~/.bashrc
echo 'eval "$(pyenv virtualenv-init -)"' >> ~/.bashrc
source ~/.bashrc

3. 安装 Python 3.8.18

pyenv install 3.8.18

4. 在 Yocto build 目录锁定版本

cd ~/linux2/yangx/myfile/fsl-arm-yocto-bsp/build
echo "3.8.18" > .python-version

5. 验证

cd ~/linux2/yangx/myfile/fsl-arm-yocto-bsp/build
python --version
# 应输出: Python 3.8.18

6. 编译

cd ~/linux2/yangx/myfile/fsl-arm-yocto-bsp
source setup-environment build
bitbake iptables

原理

  • build/.python-version 文件让 pyenv 在该目录下自动切换到 Python 3.8
  • 不影响系统其他位置的 Python 3.10
  • 不需要修改系统 Python 或使用虚拟环境
  • 不需要 source pyenv 以外的额外步骤

其他方案对比

方案 操作 评价
pyenv(推荐) 安装 3.8,echo "3.8.18" > build/.python-version ✅ 隔离性最好
修补 bitbake sed -i 's/from collections import/from collections.abc import/' ❌ 手动改源码,不优雅
系统级降级 卸载 Python 3.10 ❌ 影响其他应用
posted @ 2026-07-02 10:53  杨旭0324  阅读(4)  评论(0)    收藏  举报