Home > Software engineering >  Importing modules from parent folder does not work when running flask app
Importing modules from parent folder does not work when running flask app

Time:02-05

I am trying to import module from file in my parent directory but is not working

Here is how my directory looks like :

enter image description here

Here is how my main.py file looks like :

import sys
path_modules="./Utils"
sys.path.append(path_modules)
from functools import lru_cache

from flask import Flask, render_template, request



import Utils.HardSoftClassifier as cl
import Utils.prediction_utils as ut
import Utils.DataPrepper_reg as prep

app = Flask(__name__)



@app.route('/')
@app.route('/index')
def index():
    return render_template('index.html')


@app.route('/viz')
def viz():
    return render_template('viz.html')
    


if __name__=="__main__":
#    import os
#    os.system("start microsoft-edge:http:\\localhost:5000/")
    app.run(debug=True)

Here is my flaks output :

enter image description here

CodePudding user response:

In order to make a package importable, it must have an (optionally empty) __init__.py file in its root. So first create an empty __init__.py file in the Utils directory.

Furthermore, you don't have to alter the import path, since you start the Flask app from the Clean_code, the simple relative importing will work. So the first three line can be removed from main.py.

  •  Tags:  
  • Related