pyinstaller 打包发布flask 应用

pyinstaller 打包发布flask 应用

安装

bash 复制代码
pip install pyinstaller

入口程序,启动程序:app.py

python 复制代码
from flask import Flask

app = Flask(__name__)

@app.route('/')
def hello_world():
    return 'Hello, World!'

if __name__ == '__main__':
    app.run()

1 打包成一个二进制文件,没有静态文件和模板

bash 复制代码
pyinstaller --onefile app.py

PyInstaller 会在 dist 目录下生成一个 app 文件,直接运行

2 打包成一个软件包dist,有静态文件和模板

flaskr 文件夹下的结构:

  • instance 实例数据库

  • static 静态文件

  • templates 模版

  • uploads 上传的文件夹

bash 复制代码
flaskr/
├── a.py
├── b.py
├── c.py
├── __init__.py
├── instance
│   └── a.sqlite
├── tt
│   ├── xxx.py
├── schema.sql
├── static
│   ├── 1.css
│   ├── 2.css
│   ├── 3.css
│   ├── editormd
│   ├── images
│   │   ├── 1.jpg
│   │   ├── 2.png
│   │   ├── 3.jpg
│   │   ├── 4.jpg
│   │   └── 5.jpg
│   ├── index.css
│   └── style.css
├── templates
│   ├── a
│   │   ├── 1.html
│   │   └── 2.html
│   ├── b.html
│   └── c
│       ├── 1.html
│       ├── 2.html
│       ├── 3.html
│       ├── 4.html
│       ├── 5.html
│       └── 6.html
├── upload.py
└── uploads
    ├── txt2img-20240828-175415-0.png
    ├── txt2img-20240828-211041-0.png
    ├── txt2img-20240828-213517-0.png

目录

bash 复制代码
/proj/flaskr/
/proj/run.py

入口文件 run.py

bash 复制代码
from flaskr import create_app

app = create_app()

if __name__ == '__main__':
    app.run(debug=True)
打包:

1 配置路径:create_app 入口函数配置路径:

修改 flaskr/init.py :

python 复制代码
import os
import sys
from flask import Flask

# 指定 basedir 
if getattr(sys, 'frozen', False):  # 检查是否在打包后的环境中运行
    exedir = os.path.dirname(sys.executable)
    basedir = os.path.join(exedir,'_internal','flaskr')
else:
    basedir = os.path.abspath(os.path.dirname(__file__))

def create_app(test_config=None):
    """Create and configure an instance of the Flask application."""
    app = Flask(__name__, instance_relative_config=True)
    app.config.from_mapping(
        # a default secret that should be overridden by instance config
        SECRET_KEY="dev",
        DATABASE=os.path.join(basedir,'instance', "flaskr.sqlite"),
        UPLOAD_FOLDER=os.path.join(basedir, 'uploads'), 
        TEMPLATE_FOLDER=os.path.join(basedir, 'templates'),
        STATIC_FOLDER=os.path.join(basedir, 'static'),
    )
    
    # 省略其它代码
    
    return app

2 生成 run.spec

bash 复制代码
pyinstaller  --specpath=. run.py

3 修改run.spec 添加以下内容,把静态文件和模板文件打包进去

bash 复制代码
     datas=[
         ('flaskr/static', 'flaskr/static'),
        ('flaskr/templates', 'flaskr/templates'),
         ('flaskr/instance', 'flaskr/instance'),
         ('flaskr/uploads', 'flaskr/uploads'),
     ],

修改后完整的run.spec :

bash 复制代码
# -*- mode: python ; coding: utf-8 -*-


a = Analysis(
    ['run.py'],
    pathex=[],
    binaries=[],
    datas=[
        ('flaskr/static', 'flaskr/static'),
        ('flaskr/templates', 'flaskr/templates'),
        ('flaskr/instance', 'flaskr/instance'),
        ('flaskr/uploads', 'flaskr/uploads'),
    ],
    hiddenimports=[],
    hookspath=[],
    hooksconfig={},
    runtime_hooks=[],
    excludes=[],
    noarchive=False,
    optimize=0,
)
pyz = PYZ(a.pure)

exe = EXE(
    pyz,
    a.scripts,
    [],
    exclude_binaries=True,
    name='run',
    debug=False,
    bootloader_ignore_signals=False,
    strip=False,
    upx=True,
    console=True,
    disable_windowed_traceback=False,
    argv_emulation=False,
    target_arch=None,
    codesign_identity=None,
    entitlements_file=None,
)
coll = COLLECT(
    exe,
    a.binaries,
    a.datas,
    strip=False,
    upx=True,
    upx_exclude=[],
    name='run',
)

4 生成软件包 dist

bash 复制代码
  pyinstaller run.spec  

5 生成的dist目录结构

bash 复制代码
dist/run/run
dist/run/_internal/flaskr/
						├── instance
						│   └── a.sqlite
						├── static
						│   ├── 1.css
						│   ├── 2.css
						│   ├── 3.css
						│   ├── editormd
						│   ├── images
						│   │   ├── 1.jpg
						│   │   ├── 2.png
						│   │   ├── 3.jpg
						│   │   ├── 4.jpg
						│   │   └── 5.jpg
						│   ├── index.css
						│   └── style.css
						├── templates
						│   ├── a
						│   │   ├── 1.html
						│   │   └── 2.html
						│   ├── b.html
						│   └── c
						│       ├── 1.html
						│       ├── 2.html
						│       ├── 3.html
						│       ├── 4.html
						│       ├── 5.html
						│       └── 6.html
						├── upload.py
						└── uploads
						    ├── txt2img-20240828-175415-0.png
						    ├── txt2img-20240828-211041-0.png
						    ├── txt2img-20240828-213517-0.png

直接运行

bash 复制代码
dist/run/run
相关推荐
追逐时光者2 分钟前
Everything替代工具,一款基于 .NET 开源免费、高效且用户友好文件搜索工具!
后端·.net
kunge1v54 分钟前
学习爬虫第三天:数据提取
前端·爬虫·python·学习
爱学习的小鱼gogo7 分钟前
python 矩阵中寻找就接近的目标值 (矩阵-中等)含源码(八)
开发语言·经验分享·python·算法·职场和发展·矩阵
Hello.Reader17 分钟前
Flink 状态模式演进(State Schema Evolution)从原理到落地的一站式指南
python·flink·状态模式
红纸28117 分钟前
Subword算法之WordPiece、Unigram与SentencePiece
人工智能·python·深度学习·神经网络·算法·机器学习·自然语言处理
QX_hao17 分钟前
【Go】--数据类型
开发语言·后端·golang
红纸28122 分钟前
Subword分词方法的BPE与BBPE
人工智能·python·深度学习·神经网络·自然语言处理
桦说编程27 分钟前
线程池拒绝策略避坑:谨慎使用抛弃策略,可能导致系统卡死
java·后端
zy_destiny34 分钟前
【工业场景】用YOLOv8实现反光衣识别
人工智能·python·yolo·机器学习·计算机视觉
BingoGo1 小时前
PHP 15 个高效开发的小技巧
后端·php