Tuesday, February 6, 2024

[SOLVED] 'Flask' object has no attribute 'session_cookie_name'

Issue

I keep getting the error 'Flask' object has no attribute 'session_cookie_name' right at initialization on an app that used to work.

I built a smaller test app to test it and noticed that if I remove the line app.config["SESSION_TYPE"] = "filesystem" the error goes away. Problem is I need to use sessions in my Flask app and, since it is for testing, I would prefer to use the filesystem to store session data. here is my mini program for testing... I don't know what I am missing, this used to work fine and code examples online do it exactly this way but I keep getting the same error.

This is the error I receive

from flask import Flask, render_template,redirect, url_for, 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 index():
    session["test"] = "test"
    return render_template ('index.html')

This worked before. It is all of the sudden not working. I have tried installing a different version of Flask_Session. I have tried adding the "SESSION_COOKIE_NAME" to app.config before Session(app) even though I understand its default is session. Nothing so far has worked or changed the error message.


Solution

This error happens when the currently used version of Flask does not have the session_cookie_name attribute. This attribute was added in Flask 2.0, so if you are using an older version of Flask, you will need to upgrade to a newer version.

Just upgrade Flask by executing the command pip install --upgrade Flask

Note: If you are using a virtual environment, be sure to activate it before upgrading.



Answered By - sachin
Answer Checked By - Senaida (WPSolving Volunteer)