Alembic。 env.py中的ModuleNotFoundError

问题描述 投票:3回答:1

我有喜欢的项目结构

 --some db:
        --some db:
            --alchemy:
                -- __init__.py
            --alembic:
                -- versions
                -- env.py
                -- README.py
                -- script.py
            --migrations:
                -- __init__.py
            --models:
                -- model_1
                -- model_2
                -- __init__.py

我尝试通过alembic自动生成迁移。

我在模型文件夹的Base中有__init__.py

import sqlalchemy as sa

from sqlalchemy.orm import relationship
from sqlalchemy.ext.declarative import declarative_base, declared_attr


metadata = sa.MetaData()
Base = declarative_base(metadata=metadata)

并且将其导入为env.py

from logging.config import fileConfig

from alembic import context
from sqlalchemy import engine_from_config
from sqlalchemy import pool
from models import Base

config = context.config
fileConfig(config.config_file_name)
target_metadata = Base.metadata

因此,当我从alembic目录的env.py中的模型导入Base并尝试生成自动迁移时,我会遇到类似[]的错误>

ModuleNotFoundError:没有名为“模型”的模块

我如何解决此错误?

我有喜欢的项目结构-某些数据库:-某些数据库:-炼金术:-__init__.py --alembic:-版本-env.py ...

python sqlalchemy alembic
1个回答
0
投票
问题是执行env.py时,models不在您的PYTHONPATH中,因此无法导入。
© www.soinside.com 2019 - 2024. All rights reserved.