在开发模式下尝试 运行 sanic 时出现 ValueError

ValueError when trying to run sanic in dev mode

我正在尝试 运行 来自 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 添加一些东西吗?

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

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

D:\Whosebug\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.