【问题标题】:Flask blueprint import error : no module named烧瓶蓝图导入错误:没有模块命名
【发布时间】:2020-10-26 20:12:27
【问题描述】:

嗨,今天我开始使用烧瓶

我正在尝试配置两个蓝图

我的项目结构如下图

这是我所有的项目代码

init.py

from flask import Flask
app = Flask(__name__)

from mod_image.controllers import mod_image 
from mod_home.home import mod_home 

#the app config
#app.config.from_object('config')

#declaring image registration module/blueprint
#from app.mod_image.controllers import mod_image as image_module


# Register blueprint(s)
app.register_blueprint(mod_home)
app.register_blueprint(mod_image)



if __name__ == "__main__":
    app.run()

controllers.py

from flask import Blueprint

mod_image = Blueprint('mod_image', __name__)

@mod_image.route('/register')
def register():
    return "This is an example app"

home.py

from flask import Blueprint

mod_home = Blueprint('mod_home', __name__)

@mod_home.route('/')
def showHome():
    return "This is a home"

这是错误日志

[2020 年 7 月 6 日星期一 17:24:05.338680] [wsgi:error] [pid 15407] [client ::1:38506] mod_wsgi (pid=15407): 无法执行 Python 脚本文件 '/var/www/ wanasissmarteye/wanasissmarteye.wsgi'。

[2020 年 7 月 6 日星期一 17:24:05.338731] [wsgi:error] [pid 15407] [client ::1:38506] mod_wsgi (pid=15407): 处理 WSGI 脚本“/var/www/wanasissmarteye”时发生异常/wanasissmarteye.wsgi'。

[2020 年 7 月 6 日星期一 17:24:05.338764] [wsgi:error] [pid 15407] [client ::1:38506] Traceback(最近一次通话最后一次):

[2020 年 7 月 6 日星期一 17:24:05.338805] [wsgi:error] [pid 15407] [client ::1:38506] 文件“/var/www/wanasissmarteye/wanasissmarteye.wsgi”,第 7 行,在

[2020 年 7 月 6 日星期一 17:24:05.338860] [wsgi:error] [pid 15407] [client ::1:38506] 从 wanasissmarteye 导入​​应用程序作为应用程序

[2020 年 7 月 6 日星期一 17:24:05.338889] [wsgi:error] [pid 15407] [client ::1:38506] 文件“/var/www/wanasissmarteye/wanasissmarteye/init .py”,第 11 行,在

[2020 年 7 月 6 日星期一 17:24:05.339028] [wsgi:error] [pid 15407] [client ::1:38506] 从 mod_home.home 导入 mod_home

[Mon Jul 06 17:24:05.339063 2020] [wsgi:error] [pid 15407] [client ::1:38506] ImportError: No module named mod_home.home

【问题讨论】:

    标签: python flask


    【解决方案1】:

    python 需要 __init__.py 文件才能将您的 mod_home 和 mod_image 识别为 模块并从中导入子模块。

    在您要从中导入的每个文件夹的根目录中添加一个。

    看到这个帖子:Importing files from different folder

    【讨论】:

    • 刚刚被发现
    【解决方案2】:

    好的 因为我是 Python 的新手。 addind init.py 修复了这个问题 在我用谷歌搜索后

    init.py 文件是 Python 将目录视为包含包所必需的;

    【讨论】:

      猜你喜欢
      • 2015-05-07
      • 1970-01-01
      • 2015-02-28
      • 2019-02-10
      • 1970-01-01
      • 2017-04-07
      • 1970-01-01
      • 2020-09-14
      • 2022-08-17
      相关资源
      最近更新 更多