【问题标题】:Module Not Found error :flask module not found未找到模块错误:未找到烧瓶模块
【发布时间】:2020-04-18 09:50:30
【问题描述】:

我只是在 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'

即使我确实重新安装了软件包,但我无法解决问题

【问题讨论】:

    标签: python flask virtualenv


    【解决方案1】:

    在控制台中,当您输入py -3 App10.py 时,您可能没有使用虚拟环境的python(您安装Flask 的地方),而是默认的(您的PATH)。

    如果您在 C:\Users\lenovo\Desktop\My_Python\venv 目录中使用 virtualenv 创建了一个虚拟环境,您可能想尝试以下操作:

    C:\Users\lenovo\Desktop\My_Python\venv\Scripts\python.exe App10.py
    

    【讨论】: