6

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.

3 Answers 3

8

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.0 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.

Sign up to request clarification or add additional context in comments.

3 Comments

In version 0.5.0 of Flask-Session, the changelog states that it has: Replace use of session_cookie_name for Flask 2.3 compatibility
session_cookie_name was removed in Flask 2.3.0 (not 2.3.1): github.com/pallets/flask/blob/2.3.0/CHANGES.rst
edited per comment
1

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.

1 Comment

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.
0

Fixed with:

pip install Flask-session==0.5.0

So working env versions:

pip freeze
blinker==1.7.0
cachelib==0.10.2
certifi==2024.2.2
charset-normalizer==3.3.2
click==8.1.7
colorama==0.4.6
Flask==3.0.1
Flask-OAuthlib==0.9.6
Flask-Session==0.3.2
idna==3.6
itsdangerous==2.1.2
Jinja2==3.1.3
MarkupSafe==2.1.5
oauthlib==2.1.0
python-dateutil==2.8.2
requests==2.31.0
requests-oauthlib==1.1.0
six==1.16.0
urllib3==2.2.0
Werkzeug==3.0.1
xero_python==1.5.3

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.