sanic
sanic copied to clipboard
can not run `--dev` from SANIC CLI on windows
Describe the bug
while running sanic --dev server.app
occures an error:
Error while finding module specification for '__main__' (ValueError: __main__.__spec__ is None)
Code snippet 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.")
Expected behavior A clear and concise description of what you expected to happen.
Environment (please complete the following information):
- OS: windows 10
- Version: latest sanic
Please provide a full stack trace, how you are running python (IronPython? WSL? something else? Are you using a dependency management tool? Virtualenv?) and if possible the banner that displays when sanic starts.
I have the same problem! Was about to open a duplicate issue. Latest Sanic (22.3.1). Using Windows 11 (barely different from Windows 10 under the hood). I'm running normal cPython 3.10, installed from the Microsoft Store (once again, hasn't proved to be any different from Python from the website). No WSL or Git or Virtualenv or anything, just pure Python files with a minimal Sanic example.
@rqx110 already posted the full traceback, there basically is none. Can't post the Sanic banner either, because it never appears.
PS [my current working directory]> sanic application:app --dev
[my python path]\python.exe: Error while finding module specification for '__main__' (ValueError: __main__.__spec__ is None)
PS [my current working directory]>
This is to serve the app
variable in the application.py
file in my current working directory.
Running the same command without the --dev
flag works fine.
We'll get someone that has access to windows systems to take a look.
If I remember correctly from when I experienced this once, this would happen if my app was a package and the app instance was not imported in the package's __init__.py
.
Given the following structure:
# foo/app.py
from sanic import Sanic
app = Sanic(__name__)
...
It would raise the error message if the app reference was not imported in the __init__.py
file, but would work fine if it was (like here):
# foo/__init__.py
from .app import app
__all__ = (
"app",
)
Could you have a look @rqx110 / @nattheriddle1 and see if this is (still?) the case for you too?
Disclaimer: Wrote this out of memory. Hoping I remember correctly. 😅
Good idea, @prryplatypus, thanks for helping! Unfortunately, didn't work for me. 😔
I tested by making a separate minimal Sanic project in a different folder.
Like I said, I store my primary application file in the root of the current working directory: [my current working directory]\app.py
. I tried creating [my current working directory]\__init__.py
and adding your code (and dozens of similar iterations), but it just pounded out the same error and quit.
Then I nested these app.py
and __init__.py
files into a foo
folder: [my current working directory]\foo\app.py
.
Then I tried calling on the working directory sanic foo.app:app --dev
. Once again, no go.
Again, weirdly enough, all scenarios ran just fine by removing the --dev
flag...
I believe it's the auto-reloading part that causes it. Do you get the same error message if you replace --dev
with --auto-reload
?
After a quick search I believe the way I worked around this was by running my app with python -m sanic foo.app --dev
instead of using sanic
directly. It's possible that it only worked in combination with what I posted above, but I can't remember exactly.
@prryplatypus Yes, using the --auto-reload
flag is sufficient to produce the same error.
I just tried that workaround command on my first project (without any of the previous __init__.py
workarounds) and it worked!
The other workaround, which I have been using since I cannot use the CLI, involves setting up my application.py
file to serve the app when directly imported, like...
if __name__ == "__main__":
app.run(auto_reload=True)
...and then running python application.py
or python -m application
on the command line.
Thanks for letting us know! @ahopkins, do you agree that this can now be labelled as a bug?
I need to investigate it more first. Meaning I need to get onto my windows laptop and see why the entry point is not working and whether it is an implementation issue or an OS issue.
I believe it's the auto-reloading part that causes it. Do you get the same error message if you replace
--dev
with--auto-reload
?After a quick search I believe the way I worked around this was by running my app with
python -m sanic foo.app --dev
instead of usingsanic
directly. It's possible that it only worked in combination with what I posted above, but I can't remember exactly.
For me, python -m sanic foo.app --dev
works fine, and sanic foo.app --dev
not work.
Windows 11. Sanic 22.3.2
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If this is incorrect, please respond with an update. Thank you for your contributions.
I tried on 22.9 to reproduce this issue, but I can't reproduce it.
However, it was reproducible in 22.3.2
Perhaps it was fixed due to a refactoring of the worker.
Thanks for looking at this.
@SaidBySolo I think you are right. Can't reproduce with sanic-22.9.1 and Windows 11. But the issue is existed in the 21.12LTS.