Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flask ImportError: No Module Named Flask

Tags:

python

flask

Try deleting the virtualenv you created. Then create a new virtualenv with:

virtualenv flask

Then:

cd flask

Now let's activate the virtualenv

source bin/activate

Now you should see (flask) on the left of the command line.

Edit: In windows there is no "source" that's a linux thing, instead execute the activate.bat file, here I do it using Powershell: PS C:\DEV\aProject> & .\Flask\Scripts\activate)

Let's install flask:

pip install flask

Then create a file named hello.py (NOTE: see UPDATE Flask 1.0.2 below):

from flask import Flask
app = Flask(__name__)

@app.route("/")
def hello():
    return "Hello World!"

if __name__ == "__main__":
    app.run()

and run it with:

python hello.py

UPDATE Flask 1.0.2

With the new flask release there is no need to run the app from your script. hello.py should look like this now:

from flask import Flask
app = Flask(__name__)

@app.route("/")
def hello():
    return "Hello World!"

and run it with:

FLASK_APP=hello.py flask run

Make sure to be inside the folder where hello.py is when running the latest command.

All the steps before the creation of the hello.py apply for this case as well


For python 3 use

pip3 install flask


The only way I could solve was by adding my users python dir to myapp.wsgi file. As an example:

sys.path.append('/home/deployer/anaconda3/lib/python3.5/site-packages')

I guess that if you install the packages in the global enviroment, you should have no problem, but I had my python packages installed as user.


After activating the virtual environment and installing Flask, I created an app.py file. I run it like this : python -m flask run. Hope this will help!


I had a similar problem with flasgger.

The reason for that was that I always use

sudo pip install flask

but for some reason that's not always the way to go. Sometimes, you have to do just

pip install flask

Another gotcha is that sometimes people type pip install Flask with the cap F

Posting this here in case somebody gets stuck. Let me know if it helped.

Useful Link: What is the difference between pip install and sudo pip install?


this is what worked for me,

sudo -H pip install flask

Or for pip3(python3) use :

sudo -H pip3 install flask

Sidenote

If you're using virtualenv it's a good idea to pip freeze >> requirements.txt to allow for the installed packages to be listed in one place. The sudo command and -H flag. For more on sudo's -H flag, look at Paul's answer. Hope this helps you.


Another thing - if you're using python3, make sure you are starting your server with python3 server.py, not python server.py