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 分钟前
A2A2A(多智能体协作)
开发语言·python·ai·ai编程
Cc.Y4 分钟前
Java零基础入门:类与对象深度掌握 —— 从“零散变量“到“打包管理“
java·开发语言·python
专业程序开发源9 分钟前
springboot生命故事书制作小程序65290-计算机课程设计、毕业设计
java·vue.js·spring boot·后端·mysql·小程序·课程设计
AINative软件工程15 分钟前
LLM Prompt Registry 工程实践:集中管理 Prompt,让模型调用不再散落在代码各处
后端·python·llm
Patrick在香港23 分钟前
MCP 的 initialize 握手真的没了?67 行标准库实测 2026-07-28 规范
python·agent·claude·mcp·json-rpc
IT_陈寒23 分钟前
SpringBoot自动配置的坑我帮你踩过了
前端·人工智能·后端
vx_Biye_Design25 分钟前
springboot角色扮演服务平台65161-计算机课程设计、毕业设计
java·vue.js·spring boot·后端·python·spring·课程设计
追烽少年x28 分钟前
从零构建一个3D点云预览器:Python + PySide6 + pyqtgraph 实战
python·3d
vx_Biye_Design37 分钟前
springboot咖啡厅顾客点单管理系统12080-计算机课程设计、毕业设计
java·vue.js·spring boot·后端·spring·课程设计·express
weixin_4404016942 分钟前
质朴的爬虫+数据处理
爬虫·python·数据分析·pandas