ImportError: FastAPI-users

6.8k Views Asked by At

From https://frankie567.github.io/fastapiusers/ - has anyone else got this to work with tortoise ORM? Can you share the config with me as I am getting this error:

module = importlib.import_module(module_str)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/importlib/init.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "", line 1006, in _gcd_import
File "", line 983, in _find_and_load
File "", line 967, in _find_and_load_unlocked
File "", line 677, in _load_unlocked
File "", line 728, in exec_module
File "", line 219, in _call_with_frames_removed
File "./main.py", line 4, in
from fastapi_users import FastAPIUsers, models
File "./fastapi_users.py", line 7, in
from fastapi_users import models
ImportError: cannot import name 'models' from 'fastapi_users' (./fastapi_users.py)

I need to work out if you need to supply a main.py file? Is it in fast-api_users directory?

Here is my main.py file:

source

from fastapi import FastAPI
from fastapi_users import FastAPIUsers, models
from fastapi_users.authentication import JWTAuthentication
from fastapi_users.db import TortoiseBaseUserModel, TortoiseUserDatabase
from starlette.requests import Request
from tortoise.contrib.starlette import register_tortoise

DATABASE_URL = "sqlite://./test.db"
SECRET = "SECRET"


class User(models.BaseUser):
    pass


class UserCreate(User, models.BaseUserCreate):
    pass


class UserUpdate(User, models.BaseUserUpdate):
    pass


class UserDB(User, models.BaseUserDB):
    pass


class UserModel(TortoiseBaseUserModel):
    pass


user_db = TortoiseUserDatabase(UserDB, UserModel)
app = FastAPI()
register_tortoise(app, db_url=DATABASE_URL, modules={"models": ["WHAT DO I PUT HERE?"]})

auth_backends = [
    JWTAuthentication(secret=SECRET, lifetime_seconds=3600),
]

fastapi_users = FastAPIUsers(
    user_db, auth_backends, User, UserCreate, UserUpdate, UserDB, SECRET,
)
app.include_router(fastapi_users.router, prefix="/users", tags=["users"])


@fastapi_users.on_after_register()
def on_after_register(user: User, request: Request):
    print(f"User {user.id} has registered.")


@fastapi_users.on_after_forgot_password()
def on_after_forgot_password(user: User, token: str, request: Request):
    print(f"User {user.id} has forgot their password. Reset token: {token}")
1

There are 1 best solutions below

0
On

The ["WHAT DO I PUT HERE?"] should be the python-path to the module that has the user models.

So if your project looks like:

  • store/
    • users.py -> The models are defined here, for example
    • main.py
    • ...

You'd need to set it to ["store.users"]

Think of it as what you'd put if you import that module? e.g. import store.users or if flatter, possibly import users.

Please use only fully-qualified module names, and not relative ones as we'd then fail to find the module to bind models to the DB.

It's probably a good idea to use a main.py as an entry point, but It shouldn't be required.

Another example of interest could be: https://tortoise-orm.readthedocs.io/en/latest/examples/fastapi.html

There the models are in a models.py and since that is in the root, the modules={"models": ["models"]},.