PyInstaller和FastAPI(超过最大递归深度) [英] PyInstaller and FastAPI (maximum recursion depth exceeded)

查看:164
本文介绍了PyInstaller和FastAPI(超过最大递归深度)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用 fastapi 作为网络界面的python服务.我需要制作一个可执行文件,因此使用的是 PyInstaller .

I have a python service that uses fastapi as a web interface. I need to make an executable file and hence I am using PyInstaller.

我一直收到以下错误:

File "..../miniconda3/lib/python3.7/site-packages/PyInstaller/lib/modulegraph/modulegraph.py", line 2912, in _load_package
    self._load_module(fqname, fp, buf, stuff)
File "..../miniconda3/lib/python3.7/site-packages/PyInstaller/lib/modulegraph/modulegraph.py", line 2093, in _load_module
    m = self._load_package(fqname, pathname, packagepath)

RecursionError: maximum recursion depth exceeded while calling a Python object

我添加了 import sys;sys.setrecursionlimit(50000)转换为原始python文件以及规范中的文件,但仍然遇到相同的问题.

I added import sys; sys.setrecursionlimit(50000) to the original python file as well as in the spec but still getting the same problem.

有趣的是,如果我不使用 fastapi (例如,它与Flask一起使用),我不会收到该错误.看起来PyInstaller的fastapi出现问题.

The interesting thing is that I don't get that error if I don't use fastapi (It works with Flask for example). Looks like PyInstaller has some issue with fastapi.

有人知道如何解决此问题吗?

Does anybody have any idea how to solve this issue?

这是我的主要python函数中的代码:

This is the code in my main python function:

from fastapi import FastAPI
import uvicorn
app = FastAPI()

if __name__ == "__main__":
    uvicorn.run("main:app", host="0.0.0.0", workers=1, port=5000)

推荐答案

我今天也遇到了同样的问题,我发现问题出在

I also encountered the same problem today, and I found that the problem was in the Pydantic module.

我直接从存储库中重新安装了它:

I reinstalled it directly from the repository:

pip uninstall pydantic

pip install git+git://github.com/samuelcolvin/pydantic@master#egg=pydantic
# or with extras
pip install git+git://github.com/samuelcolvin/pydantic@master#egg=pydantic[email,typing_extensions]

这将解决最大递归深度问题,但在 Uvicorn :

Traceback (most recent call last):
  File "logging/config.py", line 388, in resolve
AttributeError: module 'uvicorn' has no attribute 'logging'

然后我将 Uvicorn 替换为 Uvloop ,现在效果很好.

And I replaced Uvicorn with Hypercorn + Uvloop and it works very well now.

这是我的最终代码:

from fastapi import FastAPI

app = FastAPI()


@app.get("/")
def read_root():
    return {"Hello": "World"}


if __name__ == "__main__":
    import asyncio
    import uvloop
    from hypercorn.asyncio import serve
    from hypercorn.config import Config

    config = Config()
    config.bind = ["0.0.0.0:8000"]

    asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())
    loop = asyncio.new_event_loop()
    asyncio.set_event_loop(loop)

    loop.run_until_complete(serve(app, config))

这篇关于PyInstaller和FastAPI(超过最大递归深度)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆