chris
chris

Reputation: 215

ModuleNotFoundError: No module named 'flask_session'

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())

Upvotes: 15

Views: 39551

Answers (12)

Daniel Carmona
Daniel Carmona

Reputation: 11

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

Upvotes: 1

Glen020
Glen020

Reputation: 187

Too many hours on this.

Try itsdangerous==1.1.0

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

Upvotes: 0

taaaato
taaaato

Reputation: 41

  • 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)

Upvotes: 4

DavidGaray
DavidGaray

Reputation: 26

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.

Upvotes: 0

user11466962
user11466962

Reputation: 11

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.

Upvotes: 1

Gusti Scarlett Halima
Gusti Scarlett Halima

Reputation: 399

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.

Upvotes: 29

Ajitesh Abhishek
Ajitesh Abhishek

Reputation: 21

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

Upvotes: 1

Amit
Amit

Reputation: 105

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

Upvotes: 2

Faisal Ahmed Farooq
Faisal Ahmed Farooq

Reputation: 141

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!

Upvotes: 13

Onen simon
Onen simon

Reputation: 768

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/

Upvotes: 0

Vimal Raj
Vimal Raj

Reputation: 544

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

Upvotes: 0

Michael Patterson
Michael Patterson

Reputation: 1770

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

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

Upvotes: -1

Related Questions