Python Flask as Windows Service

后端 未结 2 1134
我寻月下人不归
我寻月下人不归 2021-02-01 07:15

I am trying to get a Flask app to run as a Service in Windows. I have already tried to implement a solution as suggested here and here without success.

I ha

2条回答
  •  夕颜
    夕颜 (楼主)
    2021-02-01 08:03

    According to a Reddit post, Adding all the libraries to hiddenimports should fix your problem, I tried it myself and it did work!

    So, create a file in your project's directory, named win32_service.spec with the following content

    # -*- mode: python -*-
    
    block_cipher = None
    
    
    a = Analysis(['win32_service.py'],
                 pathex=['C:\\Users\\Win7\\Desktop\\FaaS'],
                 binaries=[],
                 datas=[],
                 hiddenimports=['win32timezone',
                                'altgraph',
                                'Click'
                                'Flask',
                                'future',
                                'itsdangerous',
                                'Jinja2',
                                'macholib',
                                'MarkupSafe',
                                'pefile',
                                'PyInstaller',
                                'pyodbc',
                                'pywin32',
                                'pywin32-ctypes',
                                'Werkzeug',],
                 hookspath=[],
                 runtime_hooks=[],
                 excludes=[],
                 win_no_prefer_redirects=False,
                 win_private_assemblies=False,
                 cipher=block_cipher,
                 noarchive=False)
    pyz = PYZ(a.pure, a.zipped_data,
                 cipher=block_cipher)
    exe = EXE(pyz,
              a.scripts,
              a.binaries,
              a.zipfiles,
              a.datas,
              [],
              name='win32_service',
              debug=False,
              bootloader_ignore_signals=False,
              strip=False,
              upx=True,
              runtime_tmpdir=None,
              console=True )
    

    Don't forget to change pathex variable

    Then instead of pyinstaller --onefile --hidden-import win32timezone win32_service.py use the following command: pyinstaller --onefile win32_service.spec

提交回复
热议问题