【问题标题】:Error during execution of console_scripts in setup.cfgsetup.cfg 中执行 console_scripts 时出错
【发布时间】:2021-09-08 11:35:08
【问题描述】:

在运行 setup.cfg 中定义的 console_script 入口点时,我收到以下错误。不知道为什么它抱怨位置参数。

start-prediction-engine
Traceback (most recent call last):
  File "/usr/local/bin/start-prediction-engine", line 8, in <module>
    sys.exit(app())
TypeError: __call__() missing 3 required positional arguments: 'scope', 'receive', and 'send'

这里是 setup.cfg 文件中的内容

[options.entry_points]
console_scripts =
  start-prediction-engine = prediction_engine.prediction_engine:app

在文件 prediction_engine.py 中

if __name__ == "__main__":
    uvicorn.run("prediction_engine:app", host="0.0.0.0", port=8888, reload=True, log_level="info")

【问题讨论】:

    标签: python-3.x fastapi uvicorn


    【解决方案1】:

    我落入了同样的陷阱。

    问题是您正在尝试将控制台脚本注册到 app 对象(我假设它是一个 FastAPI 应用实例)。

    必须将控制台脚本注册到函数。更多信息见简介here

    要解决这个问题,您可以简单地创建一个运行您的应用的函数:

    # prediction_engine.py
    def main():
        uvicorn.run("prediction_engine:app", host="0.0.0.0", port=8888, reload=True, log_level="info")
    
    if __name__ == "__main__":
        sys.exit(main())
    

    如果您总是从控制台脚本运行,则无需添加 if __name__ == "__main__": 部分,但这也可以让您通过 ie 来运行它

    python prediction_engine.py
    

    最后,您需要将 setup.cfg 中的 console_script 条目指向 main() 函数:

    # setup.cfg
    [options.entry_points]
    console_scripts =
      start-prediction-engine = prediction_engine.prediction_engine:main
    

    顺便说一下,你得到的错误

    TypeError: __call__() missing 3 required positional arguments: 'scope', 'receive', and 'send'
    

    当您尝试调用 FastAPI app 实例(即app())时会发生什么,这是生成的可执行文件所做的。

    【讨论】:

    • @ben 这最终解决了您的问题吗?
    猜你喜欢
    • 2017-08-12
    • 1970-01-01
    • 1970-01-01
    • 2018-05-13
    • 2012-02-25
    • 2018-08-05
    • 2015-01-01
    • 2019-01-23
    • 2011-08-01
    相关资源
    最近更新 更多