sword_pro 2023-02-17 12:13 采纳率: 0%
浏览 75

如何使用pyinstaller 打包uwsgi启动的flask应用

我将uwsgi的配置文件uwsgi.ini增加spec文件中的datas中后,又将uwsgi增加到binaries但是打包后,仍然无法使用uwsgi启动这个打包后的文件,请问我要怎么去打包这种uwsgi启动的flask项目。
spec文件我是这样写的

a = Analysis(
    ['tcp_server.py', 'data_struct.py', 'init_point.py', 'run.py', 'tcp_client.py', 'application/__init__.py',
    'application/api.py', 'application/config.py', 'application/database.py', 'application/function.py',
    'application/utils.py'],
    pathex=[],
    binaries=[('/home/nvidiacopy/.local/bin/uwsgi', '.')],
    datas=[('magnus', '.'),('uwsgi/uwsgi.*', 'uwsgi')],
    hiddenimports=[],
    hookspath=[],
    hooksconfig={},
    runtime_hooks=[],
    excludes=[],
    win_no_prefer_redirects=False,
    win_private_assemblies=False,
    cipher=block_cipher,
    noarchive=False,
)
  • 写回答

3条回答 默认 最新

  • 马哥python说 2023-02-18 08:01
    关注

    在使用 PyInstaller 打包 uwsgi 启动的 Flask 应用时,需要额外指定 uwsgi 可执行文件的路径,以确保打包后的应用能够正确地调用 uwsgi。
    下面是一份打包 uwsgi 启动的 Flask 应用的示例 PyInstaller spec 文件(假设应用入口文件为 app.py,uwsgi 可执行文件为 /usr/local/bin/uwsgi):

    # myapp.spec
    
    # Set the path to the uwsgi executable
    uWSGI_EXECUTABLE = '/usr/local/bin/uwsgi'
    
    # Define the PyInstaller build options
    a = Analysis(['app.py'],
                 pathex=['/path/to/app'],
                 binaries=[(uWSGI_EXECUTABLE, '.')],
                 datas=[('templates', 'templates'), ('static', 'static'), ('config.ini', '.')],
                 hiddenimports=['flask', 'flask_bootstrap', 'flask_sqlalchemy'],
                 hookspath=['.'],
                 runtime_hooks=[],
                 excludes=['tkinter', 'numpy'],
                 win_no_prefer_redirects=False,
                 win_private_assemblies=False,
                 cipher=block_cipher)
    
    # Modify the PyInstaller build options for the uWSGI executable
    for bi in a.binaries:
        if bi[0] == uWSGI_EXECUTABLE:
            bi[1] = 'uwsgi'
    
    # Create the PyInstaller spec file
    pyz = PYZ(a.pure, a.zipped_data,
              cipher=block_cipher)
    
    exe = EXE(pyz,
              a.scripts,
              a.binaries,
              a.zipfiles,
              a.datas,
              [],
              name='myapp',
              debug=False,
              bootloader_ignore_signals=False,
              strip=False,
              upx=True,
              upx_exclude=[],
              upx_debug=False,
              console=True )
    
    # Add the uwsgi executable as a data file to the final distribution
    exe.files.append(uWSGI_EXECUTABLE)
    

    在 spec 文件中,我们首先定义了 uWSGI_EXECUTABLE 变量,该变量指定了 uwsgi 可执行文件的路径。接下来,我们在 binaries 中将该可执行文件添加到了应用程序的根目录下,以确保能够正确地调用该可执行文件。
    最后,我们使用 exe.files.append() 将 uwsgi 可执行文件添加到了打包后的应用程序中,以便 uwsgi 能够正确地启动 Flask 应用。
    运行 PyInstaller 命令时,指定该 spec 文件即可:

    $ pyinstaller myapp.spec
    

    打包完成后,你应该能够在 dist 目录下找到打包好的应用程序。运行该程序时,使用 uwsgi 命令启动应用程序即可:

    $ ./myapp/uwsgi --ini uwsgi.ini
    

    注意,uwsgi.ini 文件需要放在与应用程序相同的目录下,以确保 uwsgi 能够找到该文件并正确地启动 Flask 应用。

    评论

报告相同问题?

问题事件

  • 创建了问题 2月17日

悬赏问题

  • ¥15 无法输出helloworld
  • ¥15 高通uboot 打印ubi init err 22
  • ¥20 PDF元数据中的XMP媒体管理属性
  • ¥15 R语言中lasso回归报错
  • ¥15 网站突然不能访问了,上午还好好的
  • ¥15 有没有dl可以帮弄”我去图书馆”秒选道具和积分
  • ¥15 semrush,SEO,内嵌网站,api
  • ¥15 Stata:为什么reghdfe后的因变量没有被发现识别啊
  • ¥15 振荡电路,ADS仿真
  • ¥15 关于#c语言#的问题,请各位专家解答!