0

iam trying to create a new user via fastapi-users. I used this code which worked fine in the app.py with a running project:

import contextlib

from app.databases.user import get_async_session, get_user_db
from app.schemas.schemas import UserCreate
from app.models.users import get_user_manager
from fastapi_users.exceptions import UserAlreadyExists

get_async_session_context = contextlib.asynccontextmanager(get_async_session)
get_user_db_context = contextlib.asynccontextmanager(get_user_db)
get_user_manager_context = contextlib.asynccontextmanager(get_user_manager)

async def create_user(email: str, password: str, is_superuser: bool = False):
    try:
        async with get_async_session_context() as session:
            async with get_user_db_context(session) as user_db:
                async with get_user_manager_context(user_db) as user_manager:
                    user = await user_manager.create(
                        UserCreate(
                            email=email, password=password, is_superuser=is_superuser
                        )
                    )
                    print(f"User created {user}")
    except UserAlreadyExists:
        print(f"User {email} already exists")

When i want to use it in maybe for example admin.py which would be executed manually it has problems to get the dependencies with this error:

Traceback (most recent call last):
  File "X:\Projects\test\app\startup_scripts\admin.py", line 3, in <module>
    from app.databases.user import create_db_and_tables
ModuleNotFoundError: No module named 'app'

Is there a way to import these? And when the project is not running is it even possible to create a user via this single script and the dependencies?

Thank you for your help.

Ano Nymous
  • 19
  • 4

0 Answers0