12

I have a simple python file that I am trying to set up to utilize sessions, when i run the file I am receiving the error below:

ModuleNotFoundError: No module named 'flask_session'

I believe I am importing the module properly, is there anything else that I can check to set this up properly?

from flask import Flask, render_template, request, session
from flask_session import Session

app = Flask(__name__)

app.config["SESSION_PERMANENT"] = False
app.config["SESSION_TYPE"] = "filesystem"

Session(app)

@app.route("/", methods=["GET", "POST"])
def index():
    if session.get("notes") is None:
        session["notes"] = []

    if request.method == "POST":
        note = request.form.get("note")
        session["notes"].append(note)

    return render_template("index.html", notes=notes)

Here is the traceback ( most recent call last )

File "c:\python37\lib\site-packages\flask\cli.py", line 325, in __call__
Open an interactive python shell in this frameself._flush_bg_loading_exception()

File "c:\python37\lib\site-packages\flask\cli.py", line 313, in _flush_bg_loading_exception
reraise(*exc_info)

File "c:\python37\lib\site-packages\flask\_compat.py", line 35, in reraise
raise value

File "c:\python37\lib\site-packages\flask\cli.py", line 302, in _load_app
self._load_unlocked()

File "c:\python37\lib\site-packages\flask\cli.py", line 317, in _load_unlocked
self._app = rv = self.loader()

File "c:\python37\lib\site-packages\flask\cli.py", line 372, in load_app
app = locate_app(self, import_name, name)

File "c:\python37\lib\site-packages\flask\cli.py", line 242, in locate_app
'\n\n{tb}'.format(name=module_name, tb=traceback.format_exc())
PRMoureu
  • 12,817
  • 6
  • 38
  • 48
chris
  • 185
  • 1
  • 1
  • 10

12 Answers12

24

I've first encountered this problem while doing Harvard's CS50x class. I'm using Linux and I've been using Python3, as it turns out I've installed Flask-Session for Python2. I don't know whether it also applies to Mac but I used the following instead:

$ pip3 install flask-session

Then you can check whether it's installed with pip's freeze command. After doing this my pylint in VSCode no longer gave an error.

  • THANK YOU!!! I was taking CS50 Fall 2022 course and got to Chapter 9 "Flask" and everything was going well until I got this same error. Instead of pulling my hair out and cussing for hours I did a GOOGLE search: ModuleNotFoundError: No module named 'flask_session' and found this and it worked. Thanks!!!!! – Dave Aug 27 '23 at 16:00
13

I got the flask_session module not found a number of times and it took me 2 hours to find a fix after thousands of attempts. Well, I too visited a number of sites looking for a solution but didn't get any. I got this error while I was learning the online course cs50 web development on edx.org by Harvard and when I read the forum discussion on reddit I saw comments where they mentioned Harvard used a special server to make it work and I felt the pain of not going to Harvard but two hours later I'm screaming because I just found a solution that's no where on the internet.

You should not use from flask_session import Session

Instead use from flask_session.__init__ import Session

I cannot explain the terminology as to why we do this here but you can private message me if you want to understand why we do this. Happy Coding!

4
  • Be sure to have the extension installed:

pip install Flask-Session

oficial page with instructions

  • If you get the error "ModuleNotFoundError: No module named 'werkzeug.contrib'":

pip install werkzeug==0.16.0

The latest version is the 1.0.1 but only the 0.16.0 worked.

(where I found this solution) (official page)

taaaato
  • 41
  • 2
2

simply goto terminal and type pip install flask_session , wait for installation, once done you are good to go. ( Also many other solutions do not work, look for red enter image description herehighlighted part in image. Once installed it works properly.)

Amit
  • 105
  • 1
  • 6
1

I ran into the same error.

The following worked for me.

Install the extension with the following command:

$ easy_install Flask-Session

or alternatively, if you have pip installed:

$ pip install Flask-Session
amanb
  • 5,276
  • 3
  • 19
  • 38
1

I got it for windows:

There's no the library "flask_session" (--is a directory--) in your directory ...venv\Lib\site-packages (virtual environment)

You just need copy it from C:\Users\your _user\AppData\Local\Programs\Python\Python37-32\Lib\site-packages and then paste it in venv\Lib\site-packages or where you have installed your virtual environment. Thats it and sorry for my english, greetings from Medellin Colombia.

  • Thanks, I needed this. I figured it out, but to improve a bit on the directions: I copied the files from within C:\Users\username\AppData\Local\Programs\Python\Python36\Lib\site-packages\flask_session to where I found venv (dir /s venv from C:\Users\username) to C:\Users\username\AppData\Local\Programs\Python\Python36\Lib\venv I also needed the from flask_session.__init__ import Session tip from another answer. Hope this helps someone! – JulieC Nov 01 '19 at 20:49
0

Try to do following command instead of all go to terminal and type

$ easy_install Flask-Session

If it shows an error,use sudo before it , If it shows any error please let me know also feel free to upvote

Vimal Raj
  • 474
  • 6
  • 19
0

Install the extension first using the following command $ pip install Flask-Session. You can read more on flask-session via https://pythonhosted.org/Flask-Session/

Onen simon
  • 750
  • 1
  • 13
  • 17
0

For completeness, I will share my solution:

  • In my case installed everything with pip3 to make it work with python 3.7, only worked with vscode after uninstalling python 2 from the system and reopening vscode.
DavidGaray
  • 26
  • 1
0

Too many hours on this.

Try itsdangerous==1.1.0

as per https://github.com/fengsp/flask-session/issues/89

Glen020
  • 187
  • 1
  • 12
  • Please phrase this as an explained conditional answer, in order to avoid the impression of asking a clarification question instead of answering (for which a comment should be used instead of an answer, compare https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead ). For example like "If your problem is ... then the solution is to .... because .... ." – Yunnosch Mar 22 '23 at 05:40
0

Looks like someone created a fork under Flask-Session2 that can be found in pip - Flask-Session

pip install Flask-Session2

Can be imported as follow:

from flask_session import Session
-1

Are you using this library? https://pythonhosted.org/Flask-Session/

The docs suggest importing it this way: from flask.ext.session import Session

Michael Patterson
  • 1,750
  • 13
  • 11
  • 13
    FWIW, I believe that doc is out of date. [`flask.ext.xxxx` is deprecated](http://flask.pocoo.org/docs/1.0/upgrading/#extension-imports) and should be using `flask_xxxx` instead. The path in the OPs questions Python 3.7, which `flask.ext.xxxx` does not work – Wondercricket Jul 12 '18 at 20:38
  • @Wondercricket, your suggestion finally fixed the problem for me. Thank you, so much! – HMLDude Jul 02 '19 at 20:21