Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

385
Views
FastAPI: " ImportError: attempted relative import with no known parent package"

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

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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

#from Blog import schemas, models
import schemas, models
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!