创建Alembic迁移时导入的问题
我有这样的文件结构:
├───auth_service
│ ├── alembic
│ │ |___versions
│ │ │___env.py
│ │
│ |── src
| | |___config.py
| |
| |___alembic.ini
│
├───market_service
alembic.ini 文件:
[alembic]
script_location = alembic
prepend_sys_path = .
version_path_separator = os
sqlalchemy.url = .
...
alembic/env.py 文件:
from auth_service.src.config import env_config
from auth_service.src.core.engines.postgres_engine import Base
config = context.config
config.set_main_option('sqlalchemy.url', env_config.postgres_url())
if config.config_file_name is not None:
fileConfig(config.config_file_name)
target_metadata = Base.metadata
def run_migrations_offline() -> None:
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()
...
当我尝试在 .../project/auth_service 目录下运行 alembic revision --autogenerate -m 'text' 命令时,出现了一个错误:
File "C:\Users\user\PycharmProjects\Microservices-FastAPI\auth_service\alembic\env.py", line 8, in <module>
from auth_service.src.config import env_config
ModuleNotFoundError: No module named 'auth_service'
我试着在所有相关的目录中添加 __init__.py
文件,并把导入路径从:
from auth_service.src.config import env_config
from auth_service.src.core.engines.postgres_engine import Base
改成:
from ..src.config import env_config
from ..src.core.engines.postgres_engine import Base
但这导致我出现了一个错误:
File "C:\Users\user\PycharmProjects\Microservices-FastAPI\auth_service\alembic\env.py", line 8, in <module>
from ..src.config import env_config
ImportError: attempted relative import with no known parent package
0 个回答
暂无回答