gpt4 book ai didi

python - Flask-迁移 "ModuleNotFoundError"

转载 作者:行者123 更新时间:2023-12-02 14:58:18 24 4
gpt4 key购买 nike

我正在尝试基于 SQLite 数据库在我的 Flask api 上进行迁移。这是我的项目结构

├── app.py
├── blueprints
├── conf.py
├── db.sqlite
├── __init__.py
├── migrations
├── models.py
└── templates

然后我在 app.py

中初始化我的迁移类
from flask import Flask, jsonify, request, render_template
from flask_sqlalchemy import SQLAlchemy

from conf import ErrorResponses, SuccessResponses

# initialization
app = Flask(__name__)
db = SQLAlchemy(app)
from models import User, db, BlackListToken

# extensions
from flask_migrate import Migrate
mig = Migrate(app, db)

但是当 flask db migrate -m "rm id and made name/surname nullable" 时,它无法从自定义模块导入。

Usage: flask db migrate [OPTIONS]

Error: While importing "api_flask.app", an ImportError was raised:

Traceback (most recent call last):
File "/usr/local/lib/python3.6/dist-packages/flask/cli.py", line 235, in locate_app
__import__(module_name)
File "/home/zahessi/all/bubbie_D/api_flask/app.py", line 9, in <module>
from conf import ErrorResponses, SuccessResponses
ModuleNotFoundError: No module named 'conf'

这是 models.py 的样子:

from app import db

class User(db.Model):
__tablename__ = 'users'

username = db.Column(db.String(32), index=True, unique=True, nullable=False)
email = db.Column(db.String(64), unique=True, nullable=False)
password_hash = db.Column(db.String(64), nullable=False)
name = db.Column(db.String(32))
surname = db.Column(db.String(32))
public_id = db.Column(db.String(32))
creation_date = db.Column(db.DateTime)
...

当我从自定义 py 文件中删除所有导入时,一切似乎都有效,但是,这是很多不必要的工作。你有什么建议为什么它以这种方式工作?

UPD:添加了 conf.py。它在每次本地导入时都会失败,在蓝图上也是如此,具体取决于导入的顺序。

from re import findall

class ErrorResponses:
usernameOrPasswordBlank = {
"success": False,
"error": "Username and password cannot be blank."
}
userAlreadyExists = {"success": False, "error": "Such user already exists"}
incorrectData = {
"success": False,
"error": "Incorrect username or password"
}
invalidData = {
"success": False,
"error": "Username or password is invalid"
}
noUser = {
"success": False,
"error": "There is no user with such username or public_id"
}
...

最佳答案

我可以看到您解决了问题,但我想向您展示比安装 flask-script 和创建 manage.py 文件更简单的解决方案。您只需删除与 app.py 文件处于同一级别的 __init__.py 文件。它为我解决了问题。干杯:)

关于python - Flask-迁移 "ModuleNotFoundError",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51925284/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com