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! :)

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> Commented Jan 27, 2022 at 7:09
  • @Chris I've edited my question and posted all the code along with the file structure there. Commented Jan 27, 2022 at 7:20

4 Answers 4

17

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.

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/manually Commented Jul 20, 2023 at 18:56
  • 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 link
    – Matthias
    Commented Aug 1, 2023 at 9:42
  • 1
    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" Commented Aug 1, 2023 at 13:20
  • 1
    oh you're right, sharp eye, I'll edit the answer.
    – Matthias
    Commented Aug 2, 2023 at 9:26
8

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.

0
2

Hello for me helped this way (without dot):

from models import User
## instead of 
# from .models import User
0

import schemas

it's working for me

ScreenShot of the working code

New contributor
Mohit Ranjan is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.