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
相关推荐
叫我:松哥10 分钟前
基于Python flask的医院管理学院,医生能够增加/删除/修改/删除病人的数据信息,有可视化分析
javascript·后端·python·mysql·信息可视化·flask·bootstrap
海里真的有鱼13 分钟前
Spring Boot 项目中整合 RabbitMQ,使用死信队列(Dead Letter Exchange, DLX)实现延迟队列功能
开发语言·后端·rabbitmq
工业甲酰苯胺23 分钟前
Spring Boot 整合 MyBatis 的详细步骤(两种方式)
spring boot·后端·mybatis
Eiceblue40 分钟前
Python 复制Excel 中的行、列、单元格
开发语言·python·excel
NLP工程化1 小时前
对 Python 中 GIL 的理解
python·gil
新知图书1 小时前
Rust编程的作用域与所有权
开发语言·后端·rust
极客代码1 小时前
OpenCV Python 深度指南
开发语言·人工智能·python·opencv·计算机视觉
liO_Oil1 小时前
(2024.9.19)在Python的虚拟环境中安装GDAL
开发语言·python·gdal安装
奈斯。zs1 小时前
yjs08——矩阵、数组的运算
人工智能·python·线性代数·矩阵·numpy
Melody20501 小时前
tensorflow-dataset 内网下载 指定目录
人工智能·python·tensorflow