[Python] Do "alembic current" and "alembic history" search different paths?

Discussão em 'Python' iniciado por Stack, Agosto 1, 2025.

  1. Stack

    Stack Membro Participativo

    I'm using Alembic with SQLAlchemy to do a schema migration and a data migration.

    Since I have stored migration-specific dependencies in their own module within my project's main application myapp (as per Michael Bayer's recommendation), I have run into ModuleNotFoundErrors when running alembic current and alembic history.

    As suggested here, I have added the repository directory project to my system path by adding the following to env.py.

    # Add project to path.
    sys.path.append(os.getcwd())


    This fixes the ModuleNotFoundError for alembic current, but not alembic history.

    Why do these two commands give different results?


    Repository structure:

    project
    │ alembic.ini
    │ db.sqlite
    │
    ├───alembic
    │ │ env.py
    │ │ README
    │ │ script.py.mako
    │ │
    │ └───versions
    │ 2b939015022a_create_athlete_table.py
    │ 3649f2977ae1_migrate_athletes_to_star_athletes.py
    │
    └───myapp
    └───migrations
    star_athlete.py


    Migration script:

    """migrate athletes to star_athletes

    Revision ID: 3649f2977ae1
    Revises: 2b939015022a
    Create Date: 2019-06-12 09:46:16.173048

    """
    from alembic import op
    import sqlalchemy as sa

    # This line causes ModuleNotFoundError
    from myapp.migrations.star_athlete import StarAthlete


    # revision identifiers, used by Alembic.
    revision = '3649f2977ae1'
    down_revision = '2b939015022a'
    branch_labels = None
    depends_on = None


    def upgrade():
    print('upgrading...')


    def downgrade():
    print('downgrading...')

    Continue reading...

Compartilhe esta Página