本文介绍了Pyinstaller 编译的 Uvicorn 服务器无法正确启动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我启动 server.exe 并尝试执行 uvicorn.run() 时,抛出异常:

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

请注意,uvicorn.logging 模块确实存在,当我在 Python 中执行服务器代码时,它运行正常.

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

推荐答案

我遇到了同样的问题.我发现这是hiddenimports的工作,修改xxx.spec中的以下几行很有用:

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>)

不过,还是会出现其他类似的问题.所以,我尝试添加 uvicorn 的所有文件,它适用于:

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'],

然后,运行:

pyinstaller xxx.spec

这篇关于Pyinstaller 编译的 Uvicorn 服务器无法正确启动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-10 21:28