当 运行 flask 应用程序时,从父文件夹导入模块不起作用
Importing modules from parent folder does not work when running flask app
我正在尝试从父目录中的文件导入模块,但无法正常工作
我的目录如下所示:
这是我的 main.py 文件的样子:
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)
这是我的高射炮输出:
为了 to make a package 可导入,它的根目录中必须有一个(可选为空)__init__.py
文件。所以首先在 Utils
目录中创建一个空的 __init__.py
文件。
此外,您不必更改导入路径,因为您从 Clean_code
启动 Flask 应用程序,简单的相对导入将起作用。因此可以从 main.py
.
中删除前三行
我正在尝试从父目录中的文件导入模块,但无法正常工作
我的目录如下所示:
这是我的 main.py 文件的样子:
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)
这是我的高射炮输出:
为了 to make a package 可导入,它的根目录中必须有一个(可选为空)__init__.py
文件。所以首先在 Utils
目录中创建一个空的 __init__.py
文件。
此外,您不必更改导入路径,因为您从 Clean_code
启动 Flask 应用程序,简单的相对导入将起作用。因此可以从 main.py
.