Python Forum

Full Version: Error: While importing "app", an ImportError was raised:
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Code app.py works well in a IDE online, but in my VSCODE does not work, it has the next error. What is the error??
Note: In my VSCODE I works in flask with: python -m flask run
And all my codes with FLASK works fine but this does not work.

Traceback (most recent call last):
File "C:\Users\channel66\AppData\Local\Programs\Python\Python38\lib\site-packages\flask\cli.py", line 240, in locate_app
__import__(module_name)
File "E:\flaskapplic\tasks2\app.py", line 2, in <module>
from flask_session import Session
ImportError: cannot import name 'Session' from 'flask_session' (unknown location)



app.py
from flask import Flask, render_template, redirect, request, session
from flask_session import Session

app = Flask(__name__)

app.config["SESSION_PERMANENT"] = False
app.config["SESSION_TYPE"] = "filesystem"
Session(app)

@app.route("/")
def tasks():
    if "todos" not in session: 
        session["todos"] = [] 
    return render_template("tasks.html", todos = session["todos"])   

@app.route("/add", methods = ["GET", "POST"])
def add():
    if request.method == "GET":
        return render_template("add.html")
    else:
        todo = request.form.get("task")
        session["todos"].append(todo) 
        return redirect("/")
in folder templates

add.html

{% extends "layout.html" %}

{% block body %}
<form action="/add" method="post">
<input id="task" name="task" type="text" placeholder="Task Name">
<input id="submit" type="submit" disabled>
</form>
<script>
document.getElementById("task").onkeyup = function () {
if (document.getElementById("task").value =="")
{
document.querySelector("#submit").disabled = true;
}
else {
document.querySelector("#submit").disabled = false;
}
}
</script>
{% endblock%}

layout.html

<!DOCTYPE html>
<html lang= "en">
<head>
<title>{% block title %}Tasks{% endblock %}</title>

</head>
<body>
{% block body %}
{% endblock %}
</body>
</html>


tasks.html

{% extends "layout.html" %}

{% block body %}
<h1>Tasks</h1>
<ul>
{% for todo in todos %}
<li>{{ todo }}</li>
{% endfor %}
</ul>
<a href="/add">Create a New Task</a>
{% endblock %}
The solution was very simple
VSCODE terminal ...
pip install Flask-Session

SOLVED!!! CLOSE the thread.
Get into the habit of listing the third-party libraries that your project uses in a requirements.txt file. That way you don't need to worry about remembering what they all are and which versions you're using. See this section of the pip docs.