【问题标题】:Problems in importing modules in flask app在烧瓶应用程序中导入模块的问题
【发布时间】:2020-06-25 10:49:48
【问题描述】:

我在导入模块时遇到了很多问题。 我的文件中的内容在上面:

运行.py:

from erp import app

if __name__ == '__main__':
    app.run(debug=True)

all_blueprints.py:

from flask import Flask
from flask_restful import Api
from flask_sqlalchemy import SQLAlchemy
from flask_marshmallow import Marshmallow

from blueprint_finance.all_resources import api_finance

app = Flask(__name__)
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:////tmp/test.db"
db = SQLAlchemy(app)
ma = Marshmallow(app)

app.register_blueprint(api_finance)

erp/init.py

from .all_blueprints import app, db, ma

数据库1.py

from erp import app

class Author(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(255))

我不断收到导入错误,当前是

Traceback(最近一次调用最后一次): 文件“/home/arsalan/python_practise/MY_WORK_FILES/React_works/React_Container_Mount/backend/run.py”,第 25 行,在 从 erp 导入应用程序 文件“/home/arsalan/python_practise/MY_WORK_FILES/React_works/React_Container_Mount/backend/erp/init.py”,第 1 行,在 从 .all_blueprints 导入应用程序、数据库、马 文件“/home/arsalan/python_practise/MY_WORK_FILES/React_works/React_Container_Mount/backend/erp/all_blueprints.py”,第 12 行,在 从 blueprint_finance.all_resources 导入 api_finance ModuleNotFoundError:没有名为“blueprint_finance”的模块

如果有人能指出错误,那将是很大的帮助,因为我从昨天开始就迷路了。非常感谢!

【问题讨论】:

    标签: flask python-import


    【解决方案1】:

    我认为您创建蓝图的方式不是正确的方式 (See this link for a detailed explanation)。

    如果我们指的是您构建代码的方式,

    erp/
        blueprint_finance     ---> is your blueprint package
            __init__.py       ---> is where you create your blueprint
        all_blueprints.py     ---> is where you register your blueprint
    

    让我们从erp/blueprint_finance/__init__.py 文件开始:

    from flask import Blueprint
    
    bp = Blueprint('blueprint_finance', __name__)
    
    from erp.blueprint_finance import all_resources
    from erp.blueprint_finance.finances_resources import resource1
    

    erp/all_blueprints.py

    from flask import Flask
    from flask_restful import Api
    from flask_sqlalchemy import SQLAlchemy
    from flask_marshmallow import Marshmallow
    
    app = Flask(__name__)
    app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:////tmp/test.db"
    db = SQLAlchemy(app)
    ma = Marshmallow(app)
    
    from erp.blueprint_finance import bp as blueprint_finance_bp
    app.register_blueprint(blueprint_finance_bp)
    

    【讨论】:

      猜你喜欢
      • 2012-12-01
      • 1970-01-01
      • 2015-04-18
      • 1970-01-01
      • 1970-01-01
      • 2015-09-07
      • 1970-01-01
      • 2016-12-23
      • 1970-01-01
      相关资源
      最近更新 更多