0

I am trying to set up alembic to check my db, but I am getting this error when I try to run the command mentioned below.

Folder architecture:

enter image description here

Part of the configuration (env.py) The error is on import

from logging.config import fileConfig

from sqlalchemy import engine_from_config, pool
from src.infra.sqlalchemy.config.database import Base #The error is here
from src.infra.sqlalchemy.models.models import * #The error is here

from alembic import context

# this is the Alembic Config object, which provides
# access to the values within the .ini file in use.
config = context.config

# Interpret the config file for Python logging.
# This line sets up loggers basically.
if config.config_file_name is not None:
    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

Command executed in terminal (bash) enter image description here

$ alembic revision --autogenerate -m "inicial"

Log error:

Traceback (most recent call last):
  File "C:\Users\bmirandl\AppData\Local\Programs\Python\Python39\lib\runpy.py", line 197, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "C:\Users\bmirandl\AppData\Local\Programs\Python\Python39\lib\runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "C:\Users\bmirandl\AppData\Local\Programs\Python\Python39\Scripts\alembic.exe\__main__.py", line 7, in <module>
  File "C:\Users\bmirandl\AppData\Local\Programs\Python\Python39\lib\site-packages\alembic\config.py", line 590, in main
    CommandLine(prog=prog).main(argv=argv)
  File "C:\Users\bmirandl\AppData\Local\Programs\Python\Python39\lib\site-packages\alembic\config.py", line 584, in main
    self.run_cmd(cfg, options)
  File "C:\Users\bmirandl\AppData\Local\Programs\Python\Python39\lib\site-packages\alembic\config.py", line 561, in run_cmd
    fn(
  File "C:\Users\bmirandl\AppData\Local\Programs\Python\Python39\lib\site-packages\alembic\command.py", line 229, in revision
    script_directory.run_env()
  File "C:\Users\bmirandl\AppData\Local\Programs\Python\Python39\lib\site-packages\alembic\script\base.py", line 569, in run_env
    util.load_python_file(self.dir, "env.py")
  File "C:\Users\bmirandl\AppData\Local\Programs\Python\Python39\lib\site-packages\alembic\util\pyfiles.py", line 94, in load_python_file
    module = load_module_py(module_id, path)
  File "C:\Users\bmirandl\AppData\Local\Programs\Python\Python39\lib\site-packages\alembic\util\pyfiles.py", line 110, in load_module_py
    spec.loader.exec_module(module)  # type: ignore
  File "<frozen importlib._bootstrap_external>", line 850, in exec_module
  File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
  File "alembic\env.py", line 5, in <module>
    from src.infra.sqlalchemy.models.models import *
  File "C:\backEnd_py\app_blx\app_blx\.\src\infra\sqlalchemy\models\models.py", line 1, in <module>
    from infra.sqlalchemy.config.database import Base
ModuleNotFoundError: No module named 'infra'
Bruno Lorena
  • 73
  • 1
  • 9
  • Does [this](https://stackoverflow.com/questions/15648284/alembic-alembic-revision-says-import-error) answer your question? – snakecharmerb Sep 02 '22 at 15:43
  • I tried running the command export PYTHONPATH="C:\backEnd_pyapp_blx" and then alembic revision --autogenerate -m "initial" but it didn't work. – Bruno Lorena Sep 02 '22 at 16:09

0 Answers0