pythonsanic

ValueError when trying to run sanic in dev mode


I'm trying to run a server from the sanic getting started example, here is my code in 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.")

And when I run the default command sanic server.app everything is ok:

$ 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]

But when I try to run in dev mode it throws an error:

$ 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)

Should I add something to run sanic in dev mode?


Solution

  • I'm not sure why you can't just run sanic --dev server.app. I get the same error as you.

    However, running python -m sanic --dev server.app does appear to work:

    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]
    

    I also get the text Hello, world. when opening http://localhost:8000/ in a browser.