Flask ImportError: No Module Named Flask

PythonFlask

Python Problem Overview


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:

https://stackoverflow.com/questions/24188240/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.

Python Solutions


Solution 1 - Python

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

Solution 2 - Python

For python 3 use

> pip3 install flask

>

Solution 3 - Python

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.

Solution 4 - Python

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!

Solution 5 - Python

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.

Solution 6 - Python

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: https://stackoverflow.com/questions/33068758/what-is-the-difference-between-pip-install-and-sudo-pip-install

Solution 7 - Python

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

Solution 8 - Python

This is what worked for me when I got a similar error in Windows:

  1. Install virtualenv:

    pip install virtualenv
    
  2. Create a virtualenv:

    virtualenv flask
    
  3. Navigate to Scripts and activate the virtualenv:

    activate
    
  4. Install Flask:

    python -m pip install flask
    
  5. Check if flask is installed:

    python -m pip list
    

Solution 9 - Python

  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

Solution 10 - Python

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.

Solution 11 - Python

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

Solution 12 - Python

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

Solution 13 - Python

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.

Solution 14 - Python

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.

Solution 15 - Python

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.

Solution 16 - Python

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

Solution 17 - Python

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```

Solution 18 - Python

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

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

Solution 19 - Python

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.

Solution 20 - Python

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.

Before running your program and inside a VENV, try running the following command:

export PYTHONPATH="path/my_user/code"

Solution 21 - Python

In your case , the solution is :

First:

Open the terminal and navigate to the directory/folder where that python file is located ( in ur case the python file you created is run.py) and make sure you have installed flask in that same directory/folder.

Second :

Now , type the following command :

$python3 "pythonfilename" (in ur case, its $python3 "run.py")

suuceessful

Solution 22 - Python

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionanon_sweView Question on Stackoverflow
Solution 1 - PythonRollbackView Answer on Stackoverflow
Solution 2 - PythonKhalid Bin HudaView Answer on Stackoverflow
Solution 3 - PythonJulianView Answer on Stackoverflow
Solution 4 - PythonBogdan001View Answer on Stackoverflow
Solution 5 - PythonrawplutoniumView Answer on Stackoverflow
Solution 6 - Pythonaneesh joshiView Answer on Stackoverflow
Solution 7 - PythonEvanView Answer on Stackoverflow
Solution 8 - Pythonsammy ongayaView Answer on Stackoverflow
Solution 9 - PythonNingAnMeView Answer on Stackoverflow
Solution 10 - PythonNorman PilusaView Answer on Stackoverflow
Solution 11 - PythonAbidView Answer on Stackoverflow
Solution 12 - PythonMiguelView Answer on Stackoverflow
Solution 13 - PythonyunusView Answer on Stackoverflow
Solution 14 - PythonBolan OluwaView Answer on Stackoverflow
Solution 15 - PythonTao WView Answer on Stackoverflow
Solution 16 - Pythonyasin lachiniView Answer on Stackoverflow
Solution 17 - PythonSonic SoulView Answer on Stackoverflow
Solution 18 - Pythonhossein andarkhoraView Answer on Stackoverflow
Solution 19 - PythonAshish BansalView Answer on Stackoverflow
Solution 20 - PythonEnderson MenezesView Answer on Stackoverflow
Solution 21 - PythonBiku ShahView Answer on Stackoverflow
Solution 22 - PythonivanacorovicView Answer on Stackoverflow