pyinstaller后运行FastAPI多进程错误

发布于 2025-01-11 14:16:40 字数 1639 浏览 0 评论 0原文

我正在使用具有多个处理器(5 个进程)的 UVICORN 运行 python FastAPI,它从代码中运行顺利,但是当我尝试从 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".

如何引用 main:app,创建安装程序后的实际类名是什么? 我在某处读到我们需要使用像 foldername.main:app ,但这也不起作用

I am running python FastAPI with UVICORN with multiple processors (5 processes),It is running smoothly from the code, but when I tried make the exe from pyinstaller and try to run the file, it is showing error.

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

Output code from source

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.

I make a single file using pyinstaller with the following command

pyinstaller --onefile main.py

and while running the main file using

./main

get the following error

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

How to refer the main:app, what is the actual class name after installer is created?
I read somewhere that we need to use like
foldername.main:app , but that also not working

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

木森分化 2025-01-18 14:16:40

我尝试了你的程序并安装并

pyinstaller --onefile --hidden-import=main main.py

为我解决了这个问题。

I tried your program and installing with

pyinstaller --onefile --hidden-import=main main.py

solved it for me.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文