【问题标题】:ValueError when trying to run sanic in dev mode尝试在开发模式下运行 sanic 时出现 ValueError
【发布时间】:2021-12-31 10:21:57
【问题描述】:

我正在尝试从sanic getting started example 运行服务器,这是我在server.py 中的代码:

from sanic import Sanic
from sanic.response import text

app = Sanic("MyHelloWorldApp")

@app.get("/")
async def hello_world(request):
    return text("Hello, world.")

当我运行默认命令 sanic server.app 时,一切正常:

$ sanic server.app
[2021-12-31 17:08:21 +0700] [22196] [INFO] 
  ┌─────────────────────────────────────────────────────────────┐
  │                        Sanic v21.12.0                       │
  │              Goin' Fast @ http://127.0.0.1:8000             │
  ├───────────────────────┬─────────────────────────────────────┤
  │                       │     mode: production, single worker │
  │     ▄███ █████ ██     │   server: sanic                     │
  │    ██                 │   python: 3.9.4                     │
  │     ▀███████ ███▄     │ platform: Windows-10-10.0.19041-SP0 │
  │                 ██    │ packages: sanic-routing==0.7.2      │
  │    ████ ████████▀     │                                     │
  │                       │                                     │
  │ Build Fast. Run Fast. │                                     │
  └───────────────────────┴─────────────────────────────────────┘

[2021-12-31 17:08:21 +0700] [22196] [WARNING] Sanic is running in PRODUCTION mode. Consider using '--debug' or '--dev' while actively developing your application.
[2021-12-31 17:08:21 +0700] [22196] [INFO] Starting worker [22196]

但是当我尝试在开发模式下运行时,它会抛出一个错误:

$ sanic --dev server.app
Starting in v22.3, --debug will no longer automatically run the auto-reloader.
  Switch to --dev to continue using that functionality.
c:\users\ev\repos\test-dashboard\venv\scripts\python.exe: Error while finding module specification for '__main__' (ValueError: __main__.__spec__ is None)

我应该添加一些东西以在开发模式下运行 sanic 吗?

【问题讨论】:

    标签: python sanic


    【解决方案1】:

    我不确定你为什么不能只运行sanic --dev server.app。我遇到和你一样的错误。

    但是,运行 python -m sanic --dev server.app 似乎确实有效:

    D:\StackOverflow\sanic>python -m sanic --dev server.app
    Starting in v22.3, --debug will no longer automatically run the auto-reloader.
      Switch to --dev to continue using that functionality.
    [2021-12-31 11:38:07 +0000] [7696] [INFO]
      ┌────────────────────────────────────────────────────────────────┐
      │                         Sanic v21.12.0                         │
      │               Goin' Fast @ http://127.0.0.1:8000               │
      ├───────────────────────┬────────────────────────────────────────┤
      │                       │        mode: debug, single worker      │
      │     ▄███ █████ ██     │      server: sanic                     │
      │    ██                 │      python: 3.7.3                     │
      │     ▀███████ ███▄     │    platform: Windows-10-10.0.19041-SP0 │
      │                 ██    │ auto-reload: enabled                   │
      │    ████ ████████▀     │    packages: sanic-routing==0.7.2      │
      │                       │                                        │
      │ Build Fast. Run Fast. │                                        │
      └───────────────────────┴────────────────────────────────────────┘
    
    [2021-12-31 11:38:07 +0000] [7696] [DEBUG] Dispatching signal: server.init.before
    [2021-12-31 11:38:07 +0000] [7696] [DEBUG] Dispatching signal: server.init.after
    [2021-12-31 11:38:07 +0000] [7696] [INFO] Starting worker [7696]
    

    在浏览器中打开 http://localhost:8000/ 时,我也会收到文本 Hello, world.

    【讨论】:

    • 我有点担心。我以前从未见过或听说过这种行为。也许这是一个特定于窗口的问题?只是好奇,这是一个普通的pip install sanic
    • @AdamHopkins 是的,这是pip install sanic。这可能是特定于 Windows 的错误。我还必须修改 sanic/app.py 第 1556 行 (task = loop.create_task(prepped, name=name)) 以删除 name=name 参数,因为 Python 3.7 似乎不支持该参数并导致服务器无法启动。我自己没有使用 Sanic,我只是安装它来调查这个问题。
    • 非常感谢。将从这里开始调查。
    猜你喜欢
    • 2017-01-09
    • 2020-11-30
    • 2021-12-27
    • 2018-01-25
    • 1970-01-01
    • 1970-01-01
    • 2019-10-30
    • 2019-10-27
    • 2020-11-02
    相关资源
    最近更新 更多