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

I am trying to create a database from SQLAlchemy. I am using Postgres as my database. When trying to create the database I get import error in from . import models in main.py file. I am following this link

https://fastapi.tiangolo.com/tutorial/sql-databases/#create-the-database-tables

This is my file structure

Main.py

from re import S
from typing import Optional
from fastapi import Body, FastAPI,Response,status,HTTPException,Depends
from numpy import append
from pkg_resources import yield_lines
from pydantic import BaseModel
from random import  randrange
import psycopg2
from psycopg2.extras  import RealDictCursor
import time
from sqlalchemy.orm import Session
from . import models
from .database import engine,SessionLocal

models.Base.metadata.create_all(bind = engine)

app = FastAPI()

def get_db():
    db = SessionLocal()
    try:
        yield db
    finally:
        db.close()

database.py

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

SQLALCHEMY_DATABASE_URL = 'postgresql://postgres:123@Localhost/fastapi'

engine = create_engine(SQLALCHEMY_DATABASE_URL)

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

Base = declarative_base()

models.py

from sqlalchemy import Column, Integer,String,Boolean
from .database import Base
from sqlalchemy.sql.expression import null



class Post(Base):
    __tablename__ = "posts"

    id = Column(Integer,primary_key=True,nullable = False)
    title = Column(String,nullable = False)
    content = Column(String,nullable = False)
    published = Column(Boolean, default = True)

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



Solution 1:[1]

Replace: from .database import Base

Into --> from database import Base

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 monk