我正在上有关Udemy的Python课程。这是程序的后端部分,在Visual Studio代码中的虚拟环境上运行。
from flask import Flask, render_template, request
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI']='postgresql://postgres:********@localhost/height-email_collector'
db = SQLAlchemy(app)
class Data(db.Model):
__tablename__ ="data"
id =db.Column(db.Integer, primary_key=True)
email_ =db.Column(db.String(120), unique=True)
height_ =db.Column(db.Integer)
def __init__(self, email_,height_):
self.email_ = email_
self.height_ = height_
@app.route("/")
def Index():
return render_template('index.html')
@app.route("/success", methods =['POST'])
def success():
if request.method=='POST':
email = request.form["email_name"]
height = request.form["height_name"]
print(email, height)
data = Data(email,height)
db.session.add(data)
db.session.commit()
return render_template('success.html')
if __name__ == '__main__':
app.debug = True
app.run()
当我执行代码时,这就是我得到的:
(virtual) C:\Users\lenovo\Desktop\My_Python\Py_html> py -3 App10.py
Traceback (most recent call last):
File "App10.py", line 1, in <module>
from flask import Flask, render_template, request
ModuleNotFoundError: No module named 'flask'
即使我确实重新安装了软件包,但也无法解决问题
答案 0 :(得分:1)
在控制台中,当您输入py -3 App10.py
时,可能未使用虚拟环境(安装Flask的位置)的python,而是使用了默认的(PATH的)Python。
如果您在目录C:\Users\lenovo\Desktop\My_Python\venv
中使用virtualenv创建了一个虚拟环境,则可能需要尝试以下操作:
C:\Users\lenovo\Desktop\My_Python\venv\Scripts\python.exe App10.py