我正在运行具有多个处理器(5个进程)的python FastAPI和UVICORN,它从代码中平稳地运行,但是当我尝试从pyinstaller生成exe并尝试运行该文件时,它显示了错误。
文件名: main.py
import multiprocessing
import os
import uvicorn
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
async def root():
return {"message": "Hello World"}
if __name__ == "__main__":
multiprocessing.freeze_support()
print("Running the instance")
uvicorn.run("main:app", host="0.0.0.0", port=9000, workers=5)
源输出代码
python3 main.py
Running the instance
INFO: Uvicorn running on http://0.0.0.0:9000 (Press CTRL+C to quit)
INFO: Started parent process [17828]
INFO: Started server process [17869]
INFO: Waiting for application startup.
INFO: Application startup complete.
INFO: Started server process [17870]
INFO: Waiting for application startup.
INFO: Application startup complete.
我用下面的命令使用pyinstaller创建一个文件
pyinstaller --onefile main.py
在运行主文件时,使用
./main
获取以下错误
Running the instance
INFO: Uvicorn running on http://0.0.0.0:9000 (Press CTRL+C to quit)
INFO: Started parent process [18964]
ERROR: Error loading ASGI app. Could not import module "main".
ERROR: Error loading ASGI app. Could not import module "main".
如何引用主程序:app,创建安装程序后的实际类名是什么?我在某个地方读到了我们需要像foldername.main:app这样使用的东西,但这也不起作用。
发布于 2022-03-03 16:23:31
我试过你的程序并安装了
pyinstaller --onefile --hidden-import=main main.py
帮我解决了。
https://stackoverflow.com/questions/71340120
复制相似问题