You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于Yocto Kirkstone的自研Python程序打包安装方案问询

基于Yocto Kirkstone的独立Python程序安装方案

以下是符合你需求的完整实现方案,核心是利用Yocto的Python包管理类,从独立Git仓库拉取代码,通过标准setup.py自动识别文件并完成构建安装,无需手动枚举文件。

一、Python代码仓库准备(合仓示例:主程序+自研模块)

假设你的Python代码放在独立Git仓库my-python-app,结构如下:

my-python-app/
├── setup.py
├── myapp/
│   ├── __init__.py
│   ├── core/
│   │   ├── __init__.py
│   │   └── module_a.py  # 自研模块
│   └── daemon/
│       ├── __init__.py
│       └── main.py       # 后台程序入口
└── LICENSE

关键文件:setup.py

用setuptools.find_packages()自动识别所有Python包,无需手动列文件:

from setuptools import setup, find_packages

setup(
    name="myapp",
    version="0.1.0",
    packages=find_packages(),
    author="Your Name",
    license="MIT",
    # 若需将后台程序注册为系统命令,添加entry_points
    entry_points={
        "console_scripts": [
            "myapp-daemon = myapp.daemon.main:main",
        ]
    },
    # 分仓场景下,主程序依赖自研模块时添加此配置
    # install_requires=["mymodule==0.1.0"]
)

二、Yocto自定义层Recipe编写

在你的自定义层(如meta-mylayer)中创建Recipe文件recipes-python/myapp/myapp_0.1.bb:

SUMMARY = "Custom Python background application with self-developed modules"
DESCRIPTION = "Python daemon and internal modules for embedded Linux system"
HOMEPAGE = "https://your-git-repo-url.com/my-python-app"
LICENSE = "MIT"
LIC_FILES_CHKSUM = "file://LICENSE;md5=abc123def456..."  # 替换为你的LICENSE文件MD5值

# 拉取独立Git仓库代码
SRC_URI = "git://your-git-repo-url.com/my-python-app.git;branch=main;protocol=https"
# 指定固定commit哈希(推荐用固定值而非分支,保证构建可复现)
SRCREV = "a1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6q7r8s9t0"

# 继承Yocto Python包管理类,自动处理whl构建与安装
inherit python3_package

# 若需运行时依赖其他系统包,添加到RDEPENDS
# RDEPENDS:${PN} += "python3-logging python3-daemon"

# 可选:配置systemd服务启动后台程序
# inherit systemd
# SYSTEMD_SERVICE:${PN} = "myapp-daemon.service"
# FILESEXTRAPATHS:prepend := "${THISDIR}/files:"

(可选)分仓场景:自研模块独立仓库

如果自研模块与主程序分属不同仓库:

  1. 自研模块仓库my-module:编写与上述结构一致的setup.py,用find_packages()自动识别模块文件
  2. 模块Recipe编写:创建recipes-python/mymodule/mymodule_0.1.bb,内容参考上述myapp_0.1.bb,替换仓库地址、SRCREV等参数
  3. 主程序Recipe依赖调整:在myapp_0.1.bb中添加:
    DEPENDS += "python3-mymodule"
    RDEPENDS:${PN} += "python3-mymodule"
    
    同时主程序的setup.py中添加install_requires=["mymodule==0.1.0"]

三、构建验证

  1. 将自定义层添加到bblayers.conf:
    BBLAYERS += "${TOPDIR}/../meta-mylayer"
    
  2. 构建包或包含该包的镜像:
    bitbake myapp
    
  3. 部署镜像后,目标系统中:
    • Python包会被安装到/usr/lib/python3.x/site-packages/路径下
    • 若配置了entry_points,可直接执行myapp-daemon启动后台程序

内容的提问来源于stack exchange,提问作者TheAG

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.18 06:30:32