ImportError: cannot import name 'Session' from 'flask_session'
Question:
thank you in advance for the time you will invest in this issue
the use case
- I have installed Flask-Session
pip install Flask-Session
- I have copy/pasted the tutorial on https://flasksession.readthedocs.io/en/latest/
from flask import Flask, session
from flask_session import Session
app = Flask(__name__)
# Check Configuration section for more details
SESSION_TYPE = 'redis'
app.config.from_object(__name__)
Session(app)
@app.route('/set/')
def set():
session['key'] = 'value'
return 'ok'
@app.route('/get/')
def get():
return session.get('key', 'not set')
the error
python flask_session.py Traceback (most recent call last):
File "flask_session.py", line 2, in <module>
from flask_session import Session File "/flask_session.py",
line 2, in <module>
from flask_session import Session
Answers:
The issue comes from the filename: I renamed the file ‘flask_session.py’ into ‘flask_session_test.py’
The problem is flask_session persists on file-system under a folder named "flask_session" generated in current working directory… offen the same where are our main.
After that Python is unable to locate the right module due to the presence of a flask_session named folder in current path.
To solve this we must delete the folder if exists under our source path and set another name for cache folder, as follows:
from flask_session import Session
app = Flask(__name__)
app.config["SESSION_FILE_DIR"] = "./flask_session_cache"
Session(app)
thank you in advance for the time you will invest in this issue
the use case
- I have installed Flask-Session
pip install Flask-Session
- I have copy/pasted the tutorial on https://flasksession.readthedocs.io/en/latest/
from flask import Flask, session
from flask_session import Session
app = Flask(__name__)
# Check Configuration section for more details
SESSION_TYPE = 'redis'
app.config.from_object(__name__)
Session(app)
@app.route('/set/')
def set():
session['key'] = 'value'
return 'ok'
@app.route('/get/')
def get():
return session.get('key', 'not set')
the error
python flask_session.py Traceback (most recent call last):
File "flask_session.py", line 2, in <module>
from flask_session import Session File "/flask_session.py",
line 2, in <module>
from flask_session import Session
The issue comes from the filename: I renamed the file ‘flask_session.py’ into ‘flask_session_test.py’
The problem is flask_session persists on file-system under a folder named "flask_session" generated in current working directory… offen the same where are our main.
After that Python is unable to locate the right module due to the presence of a flask_session named folder in current path.
To solve this we must delete the folder if exists under our source path and set another name for cache folder, as follows:
from flask_session import Session
app = Flask(__name__)
app.config["SESSION_FILE_DIR"] = "./flask_session_cache"
Session(app)