Skip to content

Unable to run this sanic application in windows, but works fine with linux on sanic version >= 23.6.0 #3060

@mahadevkirthi

Description

@mahadevkirthi
from sanic import Sanic
from sanic.response import text

app = Sanic("helloworld")

@app.get("/")
async def hello_world(request):
    return text("Hello, World!")
    
def main():
    app.run(host="localhost", port=8080)
    
if __name__ == "__main__":
    main()

I am creating a python 3.11 virtual environment and running the below command:
helloworld

On linux, I can see hello world on http://localhost:8080

On windows, I get the below error:

Sanic app name 'helloworld' not found.
App instantiation must occur outside if __name__ == '__main__' block or by using an AppLoader.
See https://sanic.dev/en/guide/deployment/app-loader.html for more details.
Traceback (most recent call last):
  File "C:\Users\mahadevk\Downloads\helloworld_proj\py_311\Lib\site-packages\sanic\app.py", line 1633, in get_app
    return cls._app_registry[name]
           ~~~~~~~~~~~~~~~~~^^^^^^
KeyError: 'helloworld'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\mahadevk\Downloads\helloworld_proj\py_311\Lib\site-packages\sanic\worker\serve.py", line 55, in worker_serve
    app = app_loader.load()
          ^^^^^^^^^^^^^^^^^
  File "C:\Users\mahadevk\Downloads\helloworld_proj\py_311\Lib\site-packages\sanic\worker\loader.py", line 61, in load
    return self.factory()
           ^^^^^^^^^^^^^^
  File "C:\Users\mahadevk\Downloads\helloworld_proj\py_311\Lib\site-packages\sanic\app.py", line 1639, in get_app
    raise SanicException(
sanic.exceptions.SanicException: Sanic app name 'helloworld' not found.
App instantiation must occur outside if __name__ == '__main__' block or by using an AppLoader.
See https://sanic.dev/en/guide/deployment/app-loader.html for more details.
[2025-05-06 21:57:33 +0530] [1116] [ERROR] Not all workers acknowledged a successful startup. Shutting down.

One of your worker processes terminated before startup was completed. Please solve any errors experienced during startup. If you do not see an exception traceback in your error logs, try running Sanic in in a single process using --single-process or single_process=True. Once you are confident that the server is able to start without errors you can switch back to multiprocess mode.
[2025-05-06 21:57:33 +0530] [1116] [INFO] Killing Sanic-Server-0-0 [19912]
[2025-05-06 21:57:33 +0530] [1116] [INFO] Server Stopped

I also tried running as sanic helloworld:app and sanic helloworld:main, nothing seems to work on windows

If I remove the main() and add app.run in if name == "main", it works, however I am doing some custom logic in main() before calling app.run(), so I do need the main() to be called before app.run()

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions