Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

uvicorn can not start FastAPI with example settings #1495

Closed
cgi1 opened this issue May 28, 2020 · 15 comments
Closed

uvicorn can not start FastAPI with example settings #1495

cgi1 opened this issue May 28, 2020 · 15 comments
Labels
question Question or problem question-migrate

Comments

@cgi1
Copy link

cgi1 commented May 28, 2020

import uvicorn
from fastapi import FastAPI
app = FastAPI(title='MADS API')
uvicorn.run(app, host='0.0.0.0', port=8127, workers=2)
WARNING:  You must pass the application as an import string to enable 'reload' or 'workers'.

than it quits with SystemError 1

Corresponding ticket in uvicorn

Versions:

$ pip3 list |grep "uvicorn\|fastapi"
fastapi                          0.55.1       
uvicorn                          0.11.5 
@cgi1 cgi1 added the bug Something isn't working label May 28, 2020
@jorgerpo
Copy link

Why do you say it is a bug?
It is your first try at FastAPI?

@cgi1
Copy link
Author

cgi1 commented May 28, 2020

No, i upgraded from Ubuntu 18.04 to Ubuntu 20.04. It was running fine on Ubuntu 18.04. productive a long time like this. So it looks like the interfaces were changing?

@phy25
Copy link

phy25 commented May 28, 2020

You must pass the application as an import string to enable 'reload' or 'workers'.

Is the program stopping here? Looks like it's only a warning but it's very straightforward about what's going on.

@cgi1
Copy link
Author

cgi1 commented May 28, 2020

@phy25 Yes it stops here. I was searching through a bit, and found this issue report on uvicorn under Windows.

@cgi1
Copy link
Author

cgi1 commented May 29, 2020

When I try it as supposed in the related issues, it gives me errors as well:

uvicorn.run("app", host='0.0.0.0', port=8127, workers=2)
ERROR:    Error loading ASGI app. Import string "app" must be in format "<module>:<attribute>".

and

uvicorn.run("main:app", host='0.0.0.0', port=8127, workers=2)
ERROR:    Error loading ASGI app. Could not import module "main".

So uvicorn did not found the FastAPI application.

@phy25
Copy link

phy25 commented May 29, 2020

This is intended and not related to fastapi:

https://github.com/encode/uvicorn/blob/9b92925a352b9743c5cfcef4a65e74a81a1bad4f/uvicorn/main.py#L343

You need to run it with command line uvicorn if you want to run with multiple workers. Please read uvicorn documentation.

@tiangolo tiangolo added question Question or problem and removed bug Something isn't working labels Jun 6, 2020
@tiangolo
Copy link
Member

tiangolo commented Jun 6, 2020

Thanks for the help here @phy25 ! 👏 🙇

If that solves the original problem, then you can close this issue @cgi1 ✔️

@cgi1
Copy link
Author

cgi1 commented Jun 13, 2020

The answer I was searching for is here: You can actually start it the way I wanted it, but make sure to name it correctly:

uvicorn.run("main:app", host='0.0.0.0', port=8127, workers=2)
ERROR:    Error loading ASGI app. Could not import module "main".

raises Error because the file is not named main.py! If you have it as my_fastapi_server.py, you run it as follows:

uvicorn.run("my_fastapi_server:app", host='0.0.0.0', port=8127, workers=2)

@cgi1 cgi1 closed this as completed Jun 13, 2020
@mrtolkien
Copy link

mrtolkien commented Aug 11, 2020

Adding another snippet as the ones posted here were pretty cryptic for me.

With the following files:

app\main.py    # Containing an app object
debug_server.py

I needed my debug_server.py to be:

import uvicorn

# Importing app here makes the syntax cleaner as it will be picked up by refactors
from app.main import app

if __name__ == "__main__":
    uvicorn.run("debug_server:app", host="0.0.0.0", port=80, reload=True)

That way the debug auto-reload server did work properly and there’s nothing related to it in the /app folder.

@chandresholaniya
Copy link

I got following error

$ uvicorn main:app --reload
←[32mINFO←[0m: Uvicorn running on ←[1mhttp://127.0.0.1:8000←[0m (Press CTRL+C to quit)
←[32mINFO←[0m: Started reloader process [←[36m←[1m22984←[0m] using ←[36m←[1mwatchgod←[0m
←[31mERROR←[0m: Error loading ASGI app. Could not import module "main".
←[32mINFO←[0m: Stopping reloader process [←[36m←[1m22984←[0m]

I am using uvicorn [0.13.4] on Windows 10

Kindly suggest steps to resolve it.

@waketzheng
Copy link
Contributor

The answer I was searching for is here: You can actually start it the way I wanted it, but make sure to name it correctly:

uvicorn.run("main:app", host='0.0.0.0', port=8127, workers=2)
ERROR:    Error loading ASGI app. Could not import module "main".

raises Error because the file is not named main.py! If you have it as my_fastapi_server.py, you run it as follows:

uvicorn.run("my_fastapi_server:app", host='0.0.0.0', port=8127, workers=2)

You can use pathlib to avoid this problem:

from pathlib imprt Path

uvicorn.run(f"{Path(__file__).stem}:app", host='0.0.0.0', port=8127, workers=2)

@timhughes
Copy link

timhughes commented Jan 4, 2022

Try this.

import uvicorn
from fastapi import FastAPI

app = FastAPI()


@app.get("/")
def get_root():
    return {"message": "Hello World"}


if __name__ == "__main__":
    uvicorn.run("__main__:app", host="0.0.0.0", port=8000, reload=True, workers=2)

@rafaelcascalho
Copy link

What also worked for me was this:

inside the api.py file

if __name__ == "__main__":
    run(app="api:app", reload=True, port=8080, host="0.0.0.0")

@timhughes
Copy link

What also worked for me was this:

inside the api.py file

if __name__ == "__main__":
    run(app="api:app", reload=True, port=8080, host="0.0.0.0")

This depends on your file being named api.py. Using __main__.app means you can rename your app.py file to anything and it will still work.

Further reading https://docs.python.org/3/library/__main__.html

@tiangolo
Copy link
Member

tiangolo commented Nov 7, 2022

Thanks for reporting back and closing the issue @cgi1 ! 🍰

And thanks everyone else for the discussions here. ☕

@tiangolo tiangolo changed the title [BUG] uvicorn can not start FastAPI with example settings uvicorn can not start FastAPI with example settings Feb 24, 2023
@tiangolo tiangolo reopened this Feb 28, 2023
@github-actions github-actions bot removed the answered label Feb 28, 2023
@fastapi fastapi locked and limited conversation to collaborators Feb 28, 2023
@tiangolo tiangolo converted this issue into discussion #7449 Feb 28, 2023

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
question Question or problem question-migrate
Projects
None yet
Development

No branches or pull requests

9 participants