Pyinstaller-compiled Uvicorn server does not start correctly

4.8k views Asked by At

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

Traceback (most recent call last):
  File "logging\config.py", line 390, in resolve
ModuleNotFoundError: No module named 'uvicorn.logging'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "logging\config.py", line 542, in configure
  File "logging\config.py", line 654, in configure_formatter
  File "logging\config.py", line 469, in configure_custom
  File "logging\config.py", line 397, in resolve
  File "logging\config.py", line 390, in resolve
ValueError: Cannot resolve 'uvicorn.logging.DefaultFormatter': No module named 'uvicorn.logging'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "server.py", line 82, in <module>
  File "server.py", line 21, in run
  File "uvicorn\main.py", line 343, in run
  File "uvicorn\config.py", line 180, in __init__
  File "uvicorn\config.py", line 223, in configure_logging
  File "logging\config.py", line 808, in dictConfig
  File "logging\config.py", line 545, in configure
ValueError: Unable to configure formatter 'default'
[7932] Failed to execute script server

Note that the uvicorn.logging module does exist and when I perform the server`s code in Python, it operates correctly.

2

There are 2 answers

0
zinc On BEST ANSWER

I encountered the same problem. And I found it's a job of hiddenimports,It's useful to modify the following lines in xxx.spec:

a = Analysis(['xxx.py'], 
             hiddenimports=['uvicorn.logging'], 
             <everything else>)

however, there will still be other similar problems. So, I try to add all files of uvicorn,and it works with:

hiddenimports=['uvicorn.lifespan.off','uvicorn.lifespan.on','uvicorn.lifespan',
'uvicorn.protocols.websockets.auto','uvicorn.protocols.websockets.wsproto_impl',
'uvicorn.protocols.websockets_impl','uvicorn.protocols.http.auto',
'uvicorn.protocols.http.h11_impl','uvicorn.protocols.http.httptools_impl',
'uvicorn.protocols.websockets','uvicorn.protocols.http','uvicorn.protocols',
'uvicorn.loops.auto','uvicorn.loops.asyncio','uvicorn.loops.uvloop','uvicorn.loops',
'uvicorn.logging'],

then, run:

pyinstaller xxx.spec
0
rappongy On

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 <module>
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.