python - Alembic:alembic 修订版显示导入错误

标签 python flask flask-sqlalchemy alembic

我正在尝试将我的 Flask 项目与 Alembic
我的应用程序结构看起来像

project/
       configuration/
                    __init__.py
                    dev.py
                    test.py
       core/
           # all source code
       db/
         migrations/
                    __init__.py
                    alembic.ini
                    env.py
                    versions/

当我尝试从 db 目录运行以下命令时,我看到了

 File "migration/env.py", line 55, in run_migrations_online
    from configuration import app, db
ImportError: No module named configuration

我尝试了 Request a simple alembic working example for Auto Generating Migrations 中提到的解决方案,但它对我不起作用

我在 env.py run_migrations_online() 中的方法有变化是

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.

    """
    import os
    import sys

    sys.path.append(os.getcwd())
    from configuration import app, db

    alembic_config = config.get_section(config.config_ini_section)
    alembic_config['sqlalchemy.url'] = app.config['SQLALCHEMY_DATABASE_URI']
    target_metadata = db.metadata

    engine = engine_from_config(
        alembic_config,
        prefix='sqlalchemy.',
        poolclass=pool.NullPool)

    connection = engine.connect()
    context.configure(
        connection=connection,
        target_metadata=target_metadata
    )

    try:
        with context.begin_transaction():
            context.run_migrations()
    finally:
        connection.close()


if context.is_offline_mode():
    run_migrations_offline()
else:
    run_migrations_online()

我该如何解决这个问题?

最佳答案

我做了export PYTHONPATH=<path_to_project>然后再次运行命令,运行成功

关于python - Alembic:alembic 修订版显示导入错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15648284/

相关文章:

包含变量的 Python 属性名称

python - 将小时和天添加到 python 日期时间

python - Flask-SQLAlchemy:没有外键链接这些表

Nginx 权限被拒绝

python - 如何测试MongoEngine的DoesNotExist异常

python - SQLAlchemy 忽略查询中的特定字段

python - 正整数的 flask sqlalchemy 列约束

python:无法连接 'str' 和 'long' 对象

python - Flask-SQLAlchemy 多数据库并绑定(bind)

python - 试图理解 scipy 和 numpy 插值