Skip to content
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

Could not import (fastapi.)HTTPException but starlette OK #4

Closed
JSchoreels opened this issue Feb 20, 2019 · 6 comments
Closed

Could not import (fastapi.)HTTPException but starlette OK #4

JSchoreels opened this issue Feb 20, 2019 · 6 comments

Comments

@JSchoreels
Copy link

Hi tiangolo.

Thank you for your docker images

However, with this one "FROM tiangolo/uvicorn-gunicorn-fastapi:python3.7-alpine3.8"

I had to import HTTPException from starlette because it wasn't known in fastapi, in my import.

fixed for me but I thought it would be interesting for you to know

@AlexWootton
Copy link

Same for me. When running tiangolo/uvicorn-gunicorn-fastapi:python3.7, importing HTTPException from fastapi results in the following error:

ImportError: cannot import name 'HTTPException' from 'starlette' (/usr/local/lib/python3.7/site-packages/starlette/__init__.py)

To get around this I am importing directly from Starlette as @JSchoreels mentions above.

Workaround

instead of:

from fastapi import HTTPException

use:

from starlette.exceptions import HTTPException

@tiangolo
Copy link
Owner

tiangolo commented Mar 4, 2019

Thanks for the report guys. It should be fixed now.

Details

The problem was that this image had an older version of FastAPI installed. A version from before FastAPI had its own HTTPException.

But the root problem is that when I released a new version of FastAPI, this image was not updated automatically.

But I fixed it here: fastapi/fastapi#65

Now, when there's a new version of FastAPI, a build of this image will be triggered automatically, so it will always be fresh.

Please try a recent version with:

docker pull tiangolo/uvicorn-gunicorn-fastapi:python3.7-alpine3.8

and

docker pull tiangolo/uvicorn-gunicorn-fastapi:python3.7

and let me know if it's working for you.

HTTPException details:

FastAPI has its own HTTPException to be able to provide custom headers in it. Which is useful for some kinds of authentication/security. Apart from that, it's equivalent to Starlette's HTTPException (inherits from it).

In summary, you can also use Starlette's HTTPException directly and it should work well too.

@AlexWootton
Copy link

Perfect, thanks @tiangolo

For what it’s worth I decided it was probably better practice to use the Docker image that this image is based on and install FastAPI and any dependencies from a requirements.txt in one fell swoop!

Good to know that this image should always be running the latest release from now on though 👌

Oh and thanks for all your hard work. I’ve only been playing with FastAPI for a short while but from what I’ve seen of it so far it’s great! Excited to use it in a project in the near future ☺️

@tiangolo
Copy link
Owner

tiangolo commented Mar 4, 2019

@AlexWootton

For what it’s worth I decided it was probably better practice to use the Docker image that this image is based on and install FastAPI and any dependencies from a requirements.txt in one fell swoop!

I agree! In fact, I do that in the project generators: https://github.com/tiangolo/full-stack-fastapi-postgresql

Good to know that this image should always be running the latest release from now on though 👌

👌 ✔️

Oh and thanks for all your hard work. I’ve only been playing with FastAPI for a short while but from what I’ve seen of it so far it’s great! Excited to use it in a project in the near future ☺️

That's great to hear! 🎉 😄

@JSchoreels
Copy link
Author

Thank you for the feedback @tiangolo ! Closing the issue

@tiangolo
Copy link
Owner

Thanks for reporting back and closing the issue 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants