如何使用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,
)

在使用 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 应用。

要正确打包uWSGI项目,请务必提供以下文件:

  1. uwsgi.ini:用于配置uWSGI的主配置文件
  2. WSGI文件:用于指定uWSGI中的WSGI入口文件
  3. 项目文件夹:用于存储要加载的项目资源,如图片、CSS等

请确保打包中包含以上所有文件后再尝试uWSGI打包。