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
相关推荐
happylifetree14 小时前
Python70:核心语法-函数进阶-案例2
python
王中阳Go15 小时前
两个入口答案不一致先别调模型:6 行入口口径对账表(附脚本)
人工智能·后端·python·大语言模型·1024程序员节
BK贝壳15 小时前
bool-hybrid-array 落盘我只认 .bha:用 Create_BHA / Ask_BHA 做布尔数组序列化
数据结构·python·numpy·1024程序员节
Sakura_j_h_15 小时前
Python 在定时任务里输出 emoji,UnicodeEncodeError 从哪来
开发语言·字符编码·windows·python·定时任务
码事漫谈16 小时前
消除AI幻觉,普通人能做的五件事
后端
supabc12317 小时前
欧盟电子发票(XRechnung/Peppol/ZUGFeRD)踩坑记:用 Python 不装 Java 跑官方校验规则
xml·python·开源·电子发票·1024程序员节
ZhengEnCi17 小时前
AI Agent工具调用防护层怎么写-一份能直接跑的Python四层拦截实现
python·llm·agent
ZhengEnCi17 小时前
AI Agent工具误调用怎么解决-从参数校验到高危二次确认的四层工程防线
python·llm·agent
金銀銅鐵18 小时前
[Java] 用 GUI 展示 EnumSet 增减元素时的内部变化(下)
java·后端·ai编程
镜子AI19 小时前
镜子AI工程教程:用Python实现关键词布局检查器与语义改写查重(附完整代码)
人工智能·python·microsoft