Forums

Import error for flask_session

I installed the flask_session module through the console with pip:

pip install Flask-Session --user

However i still cannot import the module and it gives me an error:

ModuleNotFoundError: No module named 'flask_session'

I'm importing it with:

from flask import Flask, render_template, request, session
from flask_session import Session
app = Flask(__name__)

SESSION_TYPE = 'filesystem'
app.config.from_object(__name__)
Session(app)

Do i need anything special for modules installed with the --user option?

You're installing into Python 2.7. If you're using a different version of Python for your web app, install into the correct version - pip3.6 will install into Python 3.6 etc. If you're using a virtualenv, you will need to install into the virtualenv and not use the --user flag.

Thanks for the help, it worked.

Thanks for confirming!

I got the same errors when installing modules:

ModuleNotFoundError: No module named 'flask_session'
ModuleNotFoundError: No module named 'flask_caching'

after installation in virtualenvs with both:

(gazzette) 19:32 ~/mysite (master)$ pip3.8 install Flask-Session 
(gazzette) 19:34 ~/mysite (master)$ easy_install-3.8 Flask-Session

and

(gazzette) 19:50 ~/mysite (master)$ pip3.8 install Flask-Caching

Python versions are correct, the result of easy_install is:

Installed /home/Menante/.virtualenvs/gazzette/lib/python3.8/site-packages/Flask_Session-0.3.2-py3.8.egg

Any help is appreciated.

Make sure you have the virtualenv activated for the code that is doing the import.

The import error is occurring may be due to the virtual environment problem. You have to use the same virtual environment for any kind of operations with your web app. Try this:

  1. Go to "Web"
  2. Scroll Down to "Virtualenv:"
  3. Click on "Start a console in this virtualenv"

Now use your commands here i.e to install any module using pip or to start the Django/flask shell

Thank a lot, I logged out and logged in to pythonanywhere, and opened the console in my virtualenv as per Jamesroy's advice (without doing nothing) and now it works (magic?).

The modules were been already installed in my virtualenv (gazzette) as per above post.

In any case the error "module not found" still appears in the console into the python editor when I chick "run" button therein (perhaps because I first created such file and only later I created my virtualenv, I have no idea how to fix), but when I reload the app from Web page it works. Thanks again

The editor doesn't know which virtualenv to use when running a script (remember, you could potentially set up many of them) so you need to tell it which one to use by adding a special line to the start of the file -- this help page explains more.

Thank you, now all is ok!

Good to see that you sorted it out!