user1668814
user1668814

Reputation: 513

No module named flask using virtualenv

I am following these steps to learn flask http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-i-hello-world/page/0#comments

I ran this command to create the virtual env:

python virtualenv.py flask

When I try to start flask using the python.exe file in my project scripts directory, it says

No module named flask

My PATH is set to the python directory that virtualenv installed. Why is it not able to find flask?

I initially started with the official Flask quickstart guide and was able to get the webserver to run, but with this virtual env install it is not working.

Upvotes: 25

Views: 80757

Answers (12)

s3had
s3had

Reputation: 29

Try below lines :

$ python3.7 -m venv env 

$ source env/bin/activate

(env)$ pip install yourpackages

(env)$ python app.py

Upvotes: 1

Noceur
Noceur

Reputation: 1

For those of you on Windows who are running into this problem, have activated your venv and flask is installed in the right directory. For me I realized it was looking for flask but the file was called flask.exe. I renamed it and it worked perfectly.

Upvotes: 0

singh.jitendra
singh.jitendra

Reputation: 500

In Python 3.x

pip3 install flask

Worked fine for me.

Thanks & Regards

Upvotes: 0

Denis Rasulev
Denis Rasulev

Reputation: 4069

If nothing else helps, check that in your code it is:

from flask import Flask

I've tried many things before I've noticed my mistake. I had this in my code:

from Flask import Flask

When I have changed capitalized letter for the module name, i.e. flask then everything worked.

Upvotes: 1

jmaybe
jmaybe

Reputation: 51

I had this same problem on three Raspberry Pi units at the same time; beat my head against the wall trying to fix it for several hours (reinstall flask via pip, apt and aptitude - no joy).

Instead of:

pip install flask

I finally tried:

pip install Flask

Worked like a charm.

Upvotes: 5

Sanyam Jain
Sanyam Jain

Reputation: 3025

This problem can also arise if the port is not available. Try running on different port.

Upvotes: 2

thirdangle
thirdangle

Reputation: 21

I am running Python on windows 7. I had same problem No module named flask.

I tried reinstalling python, venv but it did not work.

Finally i run it like this

  1. Install venv the usual way
  2. go to scripts directory and activate
  3. C:\Python34\microb>c:\Python34\microb\fla\scripts\python run.py
  4. here microb is my project and fla is venv

Upvotes: 0

Martlark
Martlark

Reputation: 14581

This error can also appear if you start your Flask python server using ./run.py or similarly use file associations to start your server. Then the python command in the association will be used instead of your virtual environment python command. Use python run.py instead. See how my run.py innocently assumes /usr/bin/python?

#!/usr/bin/python
# run.py
from app import app
app.run(debug=True,host='0.0.0.0',port=5000)

Upvotes: 4

Carl James
Carl James

Reputation: 36

On Windows even if you see (virtual_env_name) in the cmd line it is possible that the virtual environment is not completely activated. Deactivate/reactivate and try again.

Upvotes: 0

mumush
mumush

Reputation: 670

Make sure you run your script after you've activated your virtualenv. On OS X you'd see (virtual_env_name) at the start of each terminal line. To do this:

cd to your virtualenv's directory and type . bin/activate

cd to the directory containing the .py file you want to run at app launch in the browser

Now enter python file_name.py, for me the file name was routes.py following this example

Upvotes: 2

sreisman
sreisman

Reputation: 668

Activate your virtual environment first with

source bin/activate envName

Then try to run your command again

Upvotes: 1

user3068848
user3068848

Reputation:

Make sure your virtualenv is activated. Then You check on the PYTHONPATH of that virtualenv. Is there a flask package (folder) installed in that directory.

If you unsure whether you have installed flask, just run the following command to see all the packages you have installed pip list or pip show flask. Do you see flask there? If not you can run pip install flask

Upvotes: 33

Related Questions