目录

基于Rye的Django项目通过Pyinstaller用Github工作流简单打包

基于Rye的Django项目通过Pyinstaller用Github工作流简单打包

前言

Rye的介绍和安装

[Rye

https://csdnimg.cn/release/blog_editor_html/release2.3.8/ckeditor/plugins/CsdnLink/icons/icon-default.png?t=P1C7 “Rye”) [Rye 完整使用教程_安装rye-CSDN博客

https://csdnimg.cn/release/blog_editor_html/release2.3.8/ckeditor/plugins/CsdnLink/icons/icon-default.png?t=P1C7 “Rye 完整使用教程_安装rye-CSDN博客”)

正文

项目建立

配置好环境后

新建文件夹

新建文件夹,进入项目

https://i-blog.csdnimg.cn/direct/ee8a6418eae545cf9e80247861332c83.png

初始化

rye init

https://i-blog.csdnimg.cn/direct/4b525371b78347d8bab7559d90a60a6d.png

下载依赖

rye sync

https://i-blog.csdnimg.cn/direct/cf5dcfa039824c2a9b77421c46ac0ed0.png

pycharm 打开项目

pycharm .

https://i-blog.csdnimg.cn/direct/9e2c75e579d647fba401a31cf3aeff74.png

安装Django

rye add django pyinstaller

https://i-blog.csdnimg.cn/direct/dd4389be13cc4bca8852815116a1ef35.png

在src目录下新建Django项目

django-admin startproject rye_dj

https://i-blog.csdnimg.cn/direct/76438e8e7828451aa08656d03bc12088.png

修改文件pyproject.toml文件如下

[project]
name = "rye-dj-demo"
version = "0.1.0"
description = "Add your description here"
authors = [
    { name = "qe-present", email = "2664481691@qq.com" }
]
dependencies = [
    "django>=5.1.6",
    "pyinstaller>=6.12.0",
]
readme = "README.md"
requires-python = ">= 3.13"

[tool.rye.scripts]
dev= "python manage.py runserver"
build="pyinstaller -F manage.py"

增加了dev脚本和bulid脚本。

进入Django项目。

运行项目

https://i-blog.csdnimg.cn/direct/a1b8ac62054840f39d256cafbf5ffffc.png

https://i-blog.csdnimg.cn/direct/5a2b9aeaab2342a8b24865ff0eafd769.png

Pyinstaller 打包

暂时什么都不写

rye run build

https://i-blog.csdnimg.cn/direct/63debe9987eb4018b33d6f2d8b37acda.png

https://i-blog.csdnimg.cn/direct/e33bf0997db349899890698bfeee2efd.png

运行打包后的manage.exe

manage.exe runserver --noreload

笔者是通过powershell 7 运行的命令,有所不同

https://i-blog.csdnimg.cn/direct/b2cea16eecc84e7490f0bc1d986ef830.png

成功。

Github工作流yaml文件的书写

总体流程

1、读取仓库的代码

2、安装rye

3、安装依赖

4、打包

5、上传

新建相关文件及文件夹

1、在根目录下新建.github文件夹

2、在.github文件夹新建workflows文件夹

3、在workdflows文件夹新建rye-dj.yaml文件

https://i-blog.csdnimg.cn/direct/16c1c5e4ab004c4394e5884b81673440.png

rye-dj.yaml内容如下

name: rye-dj  # 工作流程名称
on: push  # 触发条件 push触发
jobs:
    build:  # 任务名称
        runs-on: window-latest  # 运行环境
        steps:
        - name: 读取仓库代码
          uses: actions/checkout@v4

        - name: 安装rye
          uses: eifinger/setup-rye@v4
          with:
            enable-cache: true
            version: 'latest'

        - name: 安装依赖
          run: rye sync

        - name: 打包
          run: |
            cd src/rye_dj
            rye run build

        - name: 上传
          uses: actions/upload-artifact@v4
          with:
            name: dist
            path: src/rye_dj/dist/manage.exe

工作流打包结果

https://i-blog.csdnimg.cn/direct/d2ac9f921c2f4cb98b011491a4cc89e7.png

https://i-blog.csdnimg.cn/direct/4a77368015184942958c4809fc2cd476.png

下载运行,项目什么都没有写,因此,没有报错。以后需要配置的项目再写一篇文章。

说明一点

为什么要在src目录下新建Django项目?

亲身实践,如果不这样,会报错,不妨试试。

总结

主要是在rye环境下,github工作流对Django通过pyinstaller进行打包。