简体   繁体   English

Flask 迁移“ModuleNotFoundError”

[英]Flask-Migrate "ModuleNotFoundError"

I'm trying to make migrations on my Flask api based on SQLite db.我正在尝试基于 SQLite db 在我的 Flask api 上进行迁移。 Here is my project structure这是我的项目结构

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

Then i initialize my migrate class in the app.py然后我在 app.py 中初始化我的 migrate 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)

But it fails on importing from custom modules while flask db migrate -m "rm id and made name/surname nullable" .但是当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'

Here is how models.py looks:这是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)
    ...

When I remove all imports from custom py files, all seems to work, however, this is a lot of unnecessary work.当我从自定义 py 文件中删除所有导入时,一切似乎都有效,但是,这是很多不必要的工作。 Do you have any suggestions why does it work in that way?你有什么建议为什么它以这种方式工作?

UPD: Added conf.py . UPD:添加conf.py It fails on every local import, on blueprints as well, depending on the order of the imports.它在每次本地导入时都会失败,在蓝图上也会失败,具体取决于导入的顺序。

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"
    }
    ...

I can see that you solved the problem, but I want to show you the simpler solution than installing flask-script and creating manage.py file.我可以看到您解决了问题,但我想向您展示比安装flask-script和创建manage.py文件更简单的解决方案。 You just have to delete __init__.py file that is at the same level as app.py file.你只需要删除__init__.py文件,它是在同一水平app.py文件。 It solved the problem for me.它为我解决了这个问题。 Cheers :)干杯:)

To solve your problem I will suggest using flask_script instead flask cli.为了解决您的问题,我建议使用flask_script 代替flask cli。 Simply create manage.py file in your root directory and then do as follows:只需在您的根目录中创建 manage.py 文件,然后执行以下操作:

from flask_migrate import Migrate, MigrateCommand
from flask_script import Manager
migrate = Migrate(app, db)
manager = Manager(app)
manager.add_command('db', MigrateCommand)

To launch migration simply do: python manage.py db migrate or python3 manage.py migrate (depends on your PATH configuration)要启动迁移,只需执行: python manage.py db migratepython3 manage.py migrate (取决于您的 PATH 配置)

To the accepted answer, have in mind that Flask_Script is not longer supported.对于已接受的答案,请记住不再支持 Flask_Script。 A better solution would be to just delete the __init__.py file from your root directory and simply run the flask cli flask db upgrade .更好的解决方案是从根目录中删除__init__.py文件,然后运行 flask cli flask db upgrade

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM