FastAPI: " ImportError: attempted relative import with no known parent package"
Asked Answered
E

3

10

I am new to FastAPI and I've been having this problem with importing my other files.

I get the error:‍‍‍‍

from . import schemas
ImportError: attempted relative import with no known parent package

For context, the file I am importing from is a Folder called Blog. I saw certain StackOverflow answers saying that instead of from . import schemas I should write from Blog import schemas. And even though their solution is right and I don't get any errors while running the python program, When I try running FastAPI using uvicorn, I get this error and my localhost page doesn't load.

  File "./main.py", line 2, in <module>
from Blog import schemas
ModuleNotFoundError: No module named 'Blog'

The file structure looks like this: enter image description here

The code to the main file looks like this:

from fastapi import FastAPI
from Blog import schemas, models
from database import engine

app = FastAPI()

models.Base.metadata.create_all(engine)


@app.post('/blog')
def create(request: schemas.Blog):
    return request

schemas.py

from pydantic import BaseModel


class Blog(BaseModel):
    title: str
    body: str

database.py

from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker

SQLALCHAMY_DATABASE_URL = 'sqlite:///./blog.db'

engine = create_engine(SQLALCHAMY_DATABASE_URL, connect_args={"check_same_thread": False})

SessionLocal = sessionmaker(bind=engine, autocommit=False, autoflush=False)

Base = declarative_base()

models.py

from sqlalchemy import *
from database import Base


class Blog(Base):
    __tablename__ = 'blogs'
    id = Column(Integer, primary_key=True, index=True)
    title = Column(String)
    body = Column(String)

The SwaggerUI is not loading either.

Any help would be greatly appreciated! :)

Elemental answered 27/1, 2022 at 6:48 Comment(2)
Hi Chris, I've already done all of those as mentioned above. I am still getting the same error <b>ModuleNotFoundError: No module named 'Blog' </b>Elemental
@Chris I've edited my question and posted all the code along with the file structure there.Elemental
G
9

Since your schemas.py and models.py files are in the same directory as your main.py file, you should import those two modules as follows, instead of using from Blog import schemas, models:

import schemas, models

For more details and examples please have a look at this answer, as well as this answer and this answer.

Glazunov answered 27/1, 2022 at 7:26 Comment(0)
K
18

You can also run your app from the folder above. For example if you use uvicorn, you can do

uvicorn folder.main:app --reload

instead of

uvicorn main:app --reload

then you can keep the dot.

Koffler answered 27/4, 2023 at 16:9 Comment(4)
Why would he run [...] main:app?? The syntax of FastAPI is the same has importing in a regular Python module. "from main import app" is equal to this part " main:app ". So what you 're stating would only work if he had a module named "app" and the FastApi object is main. fastapi.tiangolo.com/deployment/manuallyHissing
because this is the syntax for running uvicorn from the command line? not sure I understand what you mean. This is shown even in the website you linkKoffler
The first part of my comment was supposed to say "why would he run [...] app:main ". Look at the syntax you have suggested. It is incorrect. OP has a module "main.py" which contins the variable "app". Your suggestion is proposing he has a module "app.py" with the variable "main"Hissing
oh you're right, sharp eye, I'll edit the answer.Koffler
G
9

Since your schemas.py and models.py files are in the same directory as your main.py file, you should import those two modules as follows, instead of using from Blog import schemas, models:

import schemas, models

For more details and examples please have a look at this answer, as well as this answer and this answer.

Glazunov answered 27/1, 2022 at 7:26 Comment(0)
B
2

For me it worked this way (without dot):

from models import User
## instead of 
# from .models import User
Blakeley answered 22/1, 2023 at 9:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.