0
votes

Hi Today I started using flask

I m trying to configure two blueprints

my project structure is as image below

enter image description here

Here is all my project codes

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"

Here is the error log

[Mon Jul 06 17:24:05.338680 2020] [wsgi:error] [pid 15407] [client ::1:38506] mod_wsgi (pid=15407): Failed to exec Python script file '/var/www/wanasissmarteye/wanasissmarteye.wsgi'.

[Mon Jul 06 17:24:05.338731 2020] [wsgi:error] [pid 15407] [client ::1:38506] mod_wsgi (pid=15407): Exception occurred processing WSGI script '/var/www/wanasissmarteye/wanasissmarteye.wsgi'.

[Mon Jul 06 17:24:05.338764 2020] [wsgi:error] [pid 15407] [client ::1:38506] Traceback (most recent call last):

[Mon Jul 06 17:24:05.338805 2020] [wsgi:error] [pid 15407] [client ::1:38506] File "/var/www/wanasissmarteye/wanasissmarteye.wsgi", line 7, in

[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] File "/var/www/wanasissmarteye/wanasissmarteye/init.py", line 11, in

[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

2 Answers

1
votes

An __init__.py file is required for python to recognize your mod_home and mod_image as modules and import submodules from them.

Add one in the root of each folder you want to import from.

See this post: Importing files from different folder

0
votes

OK since I m a newbie to Python. addind init.py fixed the issue after I googled

The init.py files are required to make Python treat the directories as containing packages;