it gives me this error when I am running the alembic --autogenerate command. it gives the error that No module name database found but database.py file exist and it gives this error.
this is my env file and an error is occurring due to this file when I am using alembic in this file the from app.models import Base from app.config import setting files are imported so it gives the error that in these files the database and config module dose not exist but it exist there. env.py:
from logging.config import fileConfig
from sqlalchemy import engine_from_config
from sqlalchemy import pool
from alembic import context
from app.models import Base
from app.config import setting
# this is the Alembic Config object, which provides
# access to the values within the .ini file in use.
config = context.config
config.set_main_option("sqlalchemy.url", f'postgresql://{setting.database_username}:{setting.database_password}@{setting.database_hostname}:{setting.database_port}/{setting.database_name}')
# config.set_main_option("sqlalchemy.url", 'postgresql://postgres:uxairkhan@localhost/alembic')
# Interpret the config file for Python logging.
# This line sets up loggers basically.
fileConfig(config.config_file_name)
# add your model's MetaData object here
# for 'autogenerate' support
# from myapp import mymodel
# target_metadata = mymodel.Base.metadata
target_metadata = Base.metadata
# other values from the config, defined by the needs of env.py,
# can be acquired:
# my_important_option = config.get_main_option("my_important_option")
# ... etc.
def run_migrations_offline():
"""Run migrations in 'offline' mode.
This configures the context with just a URL
and not an Engine, though an Engine is acceptable
here as well. By skipping the Engine creation
we don't even need a DBAPI to be available.
Calls to context.execute() here emit the given string to the
script output.
"""
url = config.get_main_option("sqlalchemy.url")
context.configure(
url=url,
target_metadata=target_metadata,
literal_binds=True,
dialect_opts={"paramstyle": "named"},
)
with context.begin_transaction():
context.run_migrations()
def run_migrations_online():
"""Run migrations in 'online' mode.
In this scenario we need to create an Engine
and associate a connection with the context.
"""
connectable = engine_from_config(
config.get_section(config.config_ini_section),
prefix="sqlalchemy.",
poolclass=pool.NullPool,
)
with connectable.connect() as connection:
context.configure(
connection=connection, target_metadata=target_metadata
)
with context.begin_transaction():
context.run_migrations()
if context.is_offline_mode():
run_migrations_offline()
else:
run_migrations_online()
database.py: this is the database.py file and in this file it says the config does not exist and the same error occurring in the models.py file also that there is no module database. from sqlalchemy import create_engine from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.orm import sessionmaker # from config import setting
# SQLALCHEMY_DATABASE_URL = 'postgres://<username>:<password>@<ip-address/hostname>/<database-name>'
# SQLALCHEMY_DATABASE_URL = f'postgresql://{setting.database_username}:{setting.database_password}@{setting.database_hostname}:{setting.database_port}/{setting.database_name}'
engine = create_engine(SQLALCHEMY_DATABASE_URL)
SessionLocal = sessionmaker(autocommit=False , autoflush=False , bind=engine)
Base = declarative_base()
def get_db():
db = SessionLocal()
try:
yield db
finally:
db.close()
ERROR:
(venv) C:\Users\Muzair\Desktop\FAwithFCC>alembic current
Traceback (most recent call last):
File "C:\Users\Muzair\AppData\Local\Programs\Python\Python310\lib\runpy.py", line 196, in _run_module_as_main
return _run_code(code, main_globals, None,
File "C:\Users\Muzair\AppData\Local\Programs\Python\Python310\lib\runpy.py", line 86, in _run_code
exec(code, run_globals)
File "C:\Users\Muzair\Desktop\FAwithFCC\venv\Scripts\alembic.exe\__main__.py", line 7, in <module>
File "C:\Users\Muzair\Desktop\FAwithFCC\venv\lib\site-packages\alembic\config.py", line 588, in main
CommandLine(prog=prog).main(argv=argv)
File "C:\Users\Muzair\Desktop\FAwithFCC\venv\lib\site-packages\alembic\config.py", line 582, in main
self.run_cmd(cfg, options)
File "C:\Users\Muzair\Desktop\FAwithFCC\venv\lib\site-packages\alembic\config.py", line 559, in run_cmd
fn(
File "C:\Users\Muzair\Desktop\FAwithFCC\venv\lib\site-packages\alembic\command.py", line 543, in current
script.run_env()
File "C:\Users\Muzair\Desktop\FAwithFCC\venv\lib\site-packages\alembic\script\base.py", line 563, in run_env
util.load_python_file(self.dir, "env.py")
File "C:\Users\Muzair\Desktop\FAwithFCC\venv\lib\site-packages\alembic\util\pyfiles.py", line 92, in load_python_file
module = load_module_py(module_id, path)
File "C:\Users\Muzair\Desktop\FAwithFCC\venv\lib\site-packages\alembic\util\pyfiles.py", line 108, in load_module_py
spec.loader.exec_module(module) # type: ignore
File "<frozen importlib._bootstrap_external>", line 883, in exec_module
File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
File "C:\Users\Muzair\Desktop\FAwithFCC\alembic\env.py", line 7, in <module>
from app.models import Base
File "C:\Users\Muzair\Desktop\FAwithFCC\.\app\models.py", line 6, in <module>
from database import Base
ModuleNotFoundError: No module named 'database'
I search for this error in the documentation and it take 3 days for me but I didn't find any solution.