3

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.

Bax66
  • 33
  • 4

2 Answers2

3

With the upgraded Flask, you also need an upgraded flask-session (which I note that you are using)

As you have discovered, Flask >= 2.3.1 removed those names. Flask-session >= 0.4 fixes that. Check your requirements.txt. In my requirements.txt it just had a minimum version for Flask (allowing it to be upgraded on a new build) but had flask-session fixed at an older version, so the mismatch broke things.

wojtow
  • 864
  • 5
  • 11
  • In version 0.5.0 of `Flask-Session`, the [changelog](https://flasksession.readthedocs.io/en/latest/changes.html#version-0-5-0) states that it has: _Replace use of session_cookie_name for Flask 2.3 compatibility_ – Daniel T Jun 07 '23 at 00:01
0

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.

sachin
  • 1,075
  • 1
  • 7
  • 11
  • 2
    Actually, I was running the latest Flask, version 2.3.1. But, I did look at the version release notes and as of 2.3.0 `The session_cookie_name, send_file_max_age_default, use_x_sendfile, propagate_exceptions, and templates_auto_reload properties on app are removed.`. So I have downgraded and it is now working. Thank you for pointing me in the right direction. Now I just need to figure out how to get the same functionality out of the latest Flask version. Thank you. – Bax66 Apr 30 '23 at 11:19