烧瓶蓝图导入错误:未命名模块

时间:2020-07-06 16:35:09

标签: python flask

嗨,今天我开始使用烧瓶

我正在尝试配置两个蓝图

我的项目结构如下图

enter image description here

这是我所有的项目代码

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"

这是错误日志

[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]追溯(最近一次通话为最后一次):

[Mon Jul 06 17:24:05.338805 2020] [wsgi:error] [pid 15407] [client :: 1:38506]文件“ /var/www/wanasissmarteye/wanasissmarteye.wsgi”,第7行,在< / p>

[来自wanasissmarteye导入应用程序作为应用程序的[Mon Jul 06 17:24:05.338860 2020] [wsgi:error] [pid 15407] [client :: 1:38506]

[Mon Jul 06 17:24:05.338889 2020] [wsgi:error] [pid 15407] [client :: 1:38506]文件“ / var / www / wanasissmarteye / wanasissmarteye / init .py”,第11行,位于

[mod Jul 06 17:24:05.339028 2020] [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:没有名为mod_home.home的模块

2 个答案:

答案 0 :(得分:1)

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

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

查看此信息:Importing files from different folder

答案 1 :(得分:0)

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

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