119

I'm following the Flask tutorial here:

http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-i-hello-world

I get to the point where I try ./run.py and I get:

Traceback (most recent call last):
  File "./run.py", line 3, in <module>
    from app import app
  File "/Users/benjaminclayman/Desktop/microblog/app/__init__.py", line 1, in <module>
    from flask import Flask
ImportError: No module named flask

This looks similar to:

ImportError: No module named flask

But their solutions aren't helpful. For reference, I do have a folder named flask which one user mentioned may cause issues.

Community
  • 1
  • 1
anon_swe
  • 6,941
  • 14
  • 68
  • 115
  • 1
    @MalikBrahimi When I run Python and then import flask and check what version I have installed, it gives back "0.10.1". So it looks like I have it installed... – anon_swe Jul 06 '15 at 18:16
  • 4
    You're saying if you run python and then do `import flask` you have no errors? Is it possible you have to versions of python on your computer? – ari Jul 06 '15 at 18:21
  • 2
    Can you insert `import sys; print sys.path` in that file (before the line where the error happens) and also run that in the interactive prompt? – ari Jul 06 '15 at 18:25
  • 1
    @ari I tried to do the virtual environment thing but may have messed it up somehow? and yes, I'm using Python 3.3 but think I have 2.7 floating around – anon_swe Jul 06 '15 at 18:50
  • 8
    You aren't using version 3, you are using version 2, and you have two copies of Python version 2 on your mac, one that came with OSX, and the other that you installed. So, all in all - it looks like you have three versions of Python; Python3, Python2 (you installed), Python2 (came with the system). Sounds like you aren't using the correct environment here; where you have installed Flask is not the system default, and this is likely the cause of your headache. – Burhan Khalid Jul 06 '15 at 19:27
  • This worked for me `python -m pip install flask` – user10930017 Sep 29 '20 at 18:40
  • It is common nowadays to use conda - so the command would be 'conda install flask' – user1854182 Feb 17 '21 at 06:24
  • rare but possible: make sure your python file is not named 'flask.py' – user1854182 Feb 17 '21 at 06:25

22 Answers22

165

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

Jeremy Thompson
  • 52,213
  • 20
  • 153
  • 256
Rollback
  • 1,773
  • 1
  • 9
  • 14
  • 3
    You can place it anywhere as long as you run it with the virtualenv enabled. – Rollback Oct 11 '17 at 13:16
  • Hi, I'm very new for python. Everything just works fine with virtualenv in terminal. But, my problem is I'm using IDE Atom... can atom run the code at the same virtualenv environment? – boog Feb 13 '19 at 07:56
62

For python 3 use

pip3 install flask

Khalid Bin Huda
  • 1,185
  • 13
  • 12
  • 4
    After changing my python version from 2.7 to 3, it worked! – Ricardo Silva Nov 28 '17 at 22:16
  • Yes this works! Instead of doing pip install flask (which is for python 2), if you are using python3, you need to use pip3 install flask or other extensions of flask. To change your python version, you can use alias python="python3". – Lulu May 06 '19 at 02:52
12

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.

Julian
  • 2,015
  • 22
  • 17
9

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?

aneesh joshi
  • 501
  • 5
  • 10
9

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!

rawplutonium
  • 342
  • 5
  • 14
Bogdan001
  • 91
  • 1
  • 1
6

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.

rawplutonium
  • 342
  • 5
  • 14
3
  1. Edit /etc/apache2/sites-available/FlaskApp.conf
  2. Add the following two lines before the "WSGIScriptAlias" line:

WSGIDaemonProcess FlaskApp python-home=/var/www/FlaskApp/FlaskApp/venv/FlaskApp WSGIProcessGroup FlaskApp

  1. Restart Apache:service apache2 restart

I'm following the Flask tutorial too.And I met the same problem.I found this way to fix it.

http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-i-hello-world

Prof. Falken
  • 22,327
  • 18
  • 94
  • 163
NingAnMe
  • 31
  • 5
3

I was using python2 but installed this: sudo apt-get install libapache2-mod-wsgi-py3

Instead of: sudo apt-get install libapache2-mod-wsgi

Correcting the installation solved the no flask problem.

Norman Pilusa
  • 337
  • 2
  • 5
  • 1
    Ah its the simple things. I had the wrong libapache mod wsgi in my dockerfile after upgrading to python 3... have a +1 – Craicerjack Sep 21 '17 at 15:16
3

In my case the solution was as simple as starting up my virtual environment like so:

$ venv/scripts/activate

It turns out I am still fresh to Python :)

Abid
  • 250
  • 6
  • 18
2

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

Evan
  • 1,156
  • 1
  • 8
  • 21
1

Go to the flask file in microblog, then activate the virtual environment with source bin/activate, then go to flask/bin and install flask, and the rest of the packages, pip install flask. You will see flask listed inside bin directory. Try to run ./run.py again from microblog (or from wherever you have the file).

CDspace
  • 2,551
  • 17
  • 31
  • 35
Miguel
  • 36
  • 2
1

Even i too suggest u virtualenv, This might also solve ur problem.

sudo apt install python-flask

If u want to deploy in productionserver then go ahead with above solution else use virtualenv.

yunus
  • 1,821
  • 1
  • 10
  • 11
1

This is what worked for me when I got a similar error in Windows; 1. Install virtualenv

pip install virtualenve
  1. Create a virtualenv

    virtualenv flask

  2. Navigate to Scripts and activate the virtualenv

    activate

  3. Install Flask

    python -m pip install flask

  4. Check if flask is installed

    python -m pip list

sammy ongaya
  • 925
  • 1
  • 11
  • 11
1

The simplest answer to this problem is specifying the correct path to flask in your environment. Make sure the script you are running knows how to get to the flask installation. Using print(sys.path) as mentioned above certainly helped to figure out the path(s) the script was using.

0

enter your python interactive mode then:

import sys

sys.path

it will print your path. Check wether flask is installed in the sys.path.

For MacOS, python path is under /opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages

But pip'll install python package by default under /Library/Python/2.7/site-packages

That's why it doesn't work for MacOS.

Tao W
  • 3
  • 2
0

The flask script is nice to start a local development server, but you would have to restart it manually after each change to your code. That is not very nice and Flask can do better. If you enable debug support the server will reload itself on code changes, and it will also provide you with a helpful debugger if things go wrong. To enable debug mode you can export the FLASK_DEBUG environment variable before running the server: forexample your file is hello.py

$ export FLASK_APP=hello.py
$ export FLASK_DEBUG=1
$ flask run
yasin lachini
  • 2,590
  • 3
  • 12
  • 38
0

in my case using Docker, my .env file was not copied, so the following env vars were not set:

.env.local: FLASK_APP=src/app.py

so in my Dockerfile i had to include:

FROM deploy as dev
COPY env ./env

which was referenced in docker-compose.yml

env_file: ./env/.env.local

another thing i had to pay attention to is the path variable to ensure my environment is used

ENV PATH $CONDA_DIR/envs/:my_environment_name_from_yml_file:/bin:$CONDA_DIR/bin:$PATH```
Sonic Soul
  • 21,043
  • 31
  • 118
  • 190
0

my answer just for any users that use Visual Studio Flesk Web project :

Just Right Click on "Python Environment" and Click to "Add Environment"

0

If you are using Pycharm then this is the virtual environment issue.

So, at the time of creating your Python project you will have to select "Existing interpreter" option -> click "system Interpreter" -> select the correct option for example "*\AppData\Local\Programs\Python\Python3.6\python.exe".

You can use 'New Virtual Env' as well, but I have just given the quick fix that should work for Pycharm users.

0

I had a similar problem using WSL and PyCharm / VSCode, the problem only occurred in the development environment and not in production. I realized that IDE's with WSL can have problems when exporting PYTHONPATH from their work directory and I started to do it manually.

-1

It worked for me after upgrading pip:

curl https://bootstrap.pypa.io/get-pip.py | python

Found that answer here: https://stackoverflow.com/a/49748494/3197202

Then I could just install flask:

pip install flask
ivanacorovic
  • 2,125
  • 3
  • 23
  • 35
-1

Install Specific Version:

pip install flash

Upgrade flash

sudo pip3 install --upgrade flash

Getting Dependency Error in Window 10 Use code: easy_install instead of pip install

easy_install flash 

Upgrade using easy install

sudo easy_install --upgrade  flash

On OSX System to install Module: Use code: brew install instead of pip install

brew install flash 

Without Using Pip :

 sudo apt-get install -y flash 

On CentOS7 or Linux Fedora:

yum -y install flash 

Or on Fedora try

sudo dnf install flash 

Command if Homebrew screws up your path on macOS:

python -m pip install flash 

For Python3 MacOs Homebrew screws

python3 -m pip install flash

Verify module from list MacOs

pip freeze | grep  flash

For Execute on Anaconda as your python package manager

 conda install -c anaconda flash