Pyinstaller-compiled Uvicorn server does not start correctly

后端 未结 2 996
小蘑菇
小蘑菇 2021-01-22 17:52

When I start the server.exe and it is trying to perform uvicorn.run(), the exception is being thrown:

<         


        
2条回答
  •  半阙折子戏
    2021-01-22 18:23

    More universal way is too specify the hook:

    # extra-hooks/hooks-uvicorn.py
    from PyInstaller.utils.hooks import collect_submodules
    
    hiddenimports = collect_submodules('uvicorn')
    

    And run pyinstaller with --additional-hooks-dir extra-hooks. For example:

    pyinstaller -y --clean --additional-hooks-dir extra-hooks main.py
    

    But!

    If you are using websockets lib just like me. You will face the error:

    File "uvicorn\protocols\websockets\websockets_impl.py", line 24, in 
    AttributeError: module 'websockets' has no attribute 'WebSocketServerProtocol'
    [1844] Failed to execute script main
    

    For this I've found another solution. Add whole uvicorn's dir itself:

    # extra-hooks/hooks-uvicorn.py
    from PyInstaller.utils.hooks import get_package_paths
    
    datas = [(get_package_paths('uvicorn')[1], 'uvicorn')]
    

    And build exe with the above command.

提交回复
热议问题