我正在研究 beanie 迁移 文档。从
models.py
文件(如 from app.models.users import User
)导入我的文档时,vs code 不会显示任何错误。但是当我运行 beanie migrate
命令时,它会抛出一条引用消息:ModuleNotFoundError: No module named app
。我在创建相应目录时放置了 __init__.py
文件。 FastAPI 与它运行良好。
以下是migrations.py文件的编写方式:
from app.models.users import User
class Forward:
...
class Backward:
...
这是我运行的命令:
beanie migrate -uri 'mongodb://user:pass@host' -db db -p relative/path/to/migrations/directory/
这是我执行后得到的:
Building migration list
Traceback (most recent call last):
File "/home/alexpy/Desktop/backend/env/bin/beanie", line 8, in <module>
sys.exit(migrations())
File "/home/alexpy/Desktop/backend/env/lib/python3.10/site-packages/click/core.py", line 1157, in __call__
return self.main(*args, **kwargs)
File "/home/alexpy/Desktop/backend/env/lib/python3.10/site-packages/click/core.py", line 1078, in main
rv = self.invoke(ctx)
File "/home/alexpy/Desktop/backend/env/lib/python3.10/site-packages/click/core.py", line 1688, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
File "/home/alexpy/Desktop/backend/env/lib/python3.10/site-packages/click/core.py", line 1434, in invoke
return ctx.invoke(self.callback, **ctx.params)
File "/home/alexpy/Desktop/backend/env/lib/python3.10/site-packages/click/core.py", line 783, in invoke
return __callback(*args, **kwargs)
File "/home/alexpy/Desktop/backend/env/lib/python3.10/site-packages/beanie/executors/migrate.py", line 200, in migrate
asyncio.run(run_migrate(settings))
File "/usr/lib/python3.10/asyncio/runners.py", line 44, in run
return loop.run_until_complete(main)
File "/usr/lib/python3.10/asyncio/base_events.py", line 649, in run_until_complete
return future.result()
File "/home/alexpy/Desktop/backend/env/lib/python3.10/site-packages/beanie/executors/migrate.py", line 110, in run_migrate
root = await MigrationNode.build(settings.path)
File "/home/alexpy/Desktop/backend/env/lib/python3.10/site-packages/beanie/migrations/runner.py", line 222, in build
).load_module((path / name).stem)
File "<frozen importlib._bootstrap_external>", line 548, in _check_name_wrapper
File "<frozen importlib._bootstrap_external>", line 1063, in load_module
File "<frozen importlib._bootstrap_external>", line 888, in load_module
File "<frozen importlib._bootstrap>", line 290, in _load_module_shim
File "<frozen importlib._bootstrap>", line 719, in _load
File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 883, in exec_module
File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
File "/home/alexpy/Desktop/backend/app/migrations/20240901100154_users_alter_name.py", line 1, in <module>
from app.models.users import User
ModuleNotFoundError: No module named 'app'
- List item
这是我的文件层次结构的样子:
app/
├── __init__.py
├── models/
│ ├── __init__.py
│ └── users.py
├── migrations/
│ ├── __init__.py
│ └── 202409011004_user_0001.py
└── main.py
我还研究了迁移示例文件。在那里,文档类在迁移文件中被重写。 每次需要迁移时,是否都需要重写migrations.py 文件中的模型类?
您可以尝试在运行命令时临时设置 PYTHONPATH:
PYTHONPATH=/home/alexpy/Desktop/backend/app beanie migrate -uri 'mongodb://user:pass@host' -db db -p app/migrations/
或者,您可以永久设置 PYTHONPATH。
作为解决方法,您还可以考虑在迁移文件中使用相对导入。例如:
from ..models.users import User