为了账号安全,请及时绑定邮箱和手机立即绑定

Flask 蓝图导入错误:没有命名的模块

Flask 蓝图导入错误:没有命名的模块

慕标琳琳 2023-02-22 17:07:48
我正在尝试配置两个蓝图我的项目结构如下图这是我所有的项目代码init.pyfrom flask import Flaskapp = 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.pyfrom flask import Blueprintmod_image = Blueprint('mod_image', __name__)@mod_image.route('/register')def register():    return "This is an example app"home.pyfrom flask import Blueprintmod_home = Blueprint('mod_home', __name__)@mod_home.route('/')def showHome():    return "This is a home"这是错误日志[Mon Jul 06 17:24:05.338680 2020] [wsgi:error] [pid 15407] [client ::1:38506] mod_wsgi (pid=15407): 无法执行 Python 脚本文件 '/var/www/wanasissmarteye/wanasissmarteye .wsgi'。[Mon Jul 06 17:24:05.338731 2020] [wsgi:error] [pid 15407] [client ::1:38506] mod_wsgi (pid=15407):处理 WSGI 脚本 '/var/www/wanasissmarteye/wanasissmarteye. wsgi'。[Mon Jul 06 17:24:05.338764 2020] [wsgi:error] [pid 15407] [client ::1:38506] Traceback(最后一次调用):[Mon Jul 06 17:24:05.338805 2020] [wsgi:error] [pid 15407] [client ::1:38506] 文件“/var/www/wanasissmarteye/wanasissmarteye.wsgi”,第 7 行,在[Mon Jul 06 17:24:05.338860 2020] [wsgi:error] [pid 15407] [client ::1:38506] from wanasissmarteye import app as application[Mon Jul 06 17:24:05.338889 2020] [wsgi:error] [pid 15407] [client ::1:38506] 文件“/var/www/wanasissmarteye/wanasissmarteye/init .py”,第 11 行,在[Mon Jul 06 17:24:05.339028 2020] [wsgi:error] [pid 15407] [client ::1:38506] from mod_home.home import mod_home[Mon Jul 06 17:24:05.339063 2020] [wsgi:error] [pid 15407] [client ::1:38506] ImportError: No module named mod_home.home
查看完整描述

2 回答

?
largeQ

TA贡献2039条经验 获得超7个赞

__init__.pypython 需要一个文件来将您的 mod_home 和 mod_image 识别为模块并从中导入子模块。

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

请参阅此帖子:从不同文件夹导入文件


查看完整回答
反对 回复 2023-02-22
?
阿晨1998

TA贡献2037条经验 获得超6个赞

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

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


查看完整回答
反对 回复 2023-02-22
  • 2 回答
  • 0 关注
  • 107 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信