Flask Importerror: No Module Named Flask

Flask ImportError: No Module Named 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

ModuleNotFoundError: No module named 'flask'

pip can for some reason point to system-wide pip (which on many systems corresponds to Python 2.7). In order to use pip from the virtualenv, use python -m pip command. The following command will do the trick:

pip uninstall flask && python -m pip install flask

Another possibility is that you installed flask via apt and not pip. Here's the difference between the two: What is the difference between `sudo apt install python3-flask` and `pip3 install Flask`?

So now the flask command is available system-wide.

If this is the case, uninstalling flask with apt and installing it with pip should do the trick:

sudo apt remove python-flask
pip install flask

(this is my guess that the apt package is called python-flask.



Related Topics



Leave a reply



Submit