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
SchröDinger's Variable: the _Class_ Cell Magically Appears If You'Re Checking for Its Presence
How to Isolate Everything Inside of a Contour, Scale It, and Test the Similarity to an Image
How to Call an Async Function Without Await
Conditionally Format Python Pandas Cell
Pygame 2 Dimensional Movement of an Enemy Towards the Player, How to Calculate X and Y Velocity
Trouble Installing Textblob with Pip
Python: Sorting Items from Top Left to Bottom Right with Opencv
Reading Data from a CSV File in Python
Recursive Function Returning None
Simple Animation Using Tkinter
Factorize a Column of Strings in Pandas
How to Get All Combinations of Length N in Python
How to Open a File Through Python
Managing Dynamic Plotting in Matplotlib Animation Module
How to Use 'Cv2.Findcontours' in Different Opencv Versions
How to Add Static(Html, CSS, Js, etc) Files in Pyinstaller to Create Standalone Exe File