9

After reading title of this post, don't try to make duplicate first because herewith content may be asked in different way. Btw, I'm very new in python and start learning now for work requirement.

here are my dependencies

virtualenv --version => 15.0.2

pip --version => 19.0.3

flask --version => 1.0.2, Python 2.7.10 (default, Aug 17 2018, 19:45:58)

python --version => 3.7.1

And, here is my source code of main.py

from flask import Flask
app = Flask(__name__)

@app.route("/")
def home():
    return "Hello, World!"

Problem is following error encountered when I render like python main.py

Traceback (most recent call last): File "main.py", line 1, in from flask import Flask ModuleNotFoundError: No module named 'flask'

But when I render like FLASK_APP=main.py flask run, it was working. Please let me know how's difference between python ... and FLASH_APP= ...

PPShein
  • 13,309
  • 42
  • 142
  • 227
  • 1
    It seems that you have nstalled flask that uses Python 2.7 version, but your default python is Python 3.7.1. Are you showing package versions from your virtualenv, or are some of these packages globally installed? – bagljas Mar 12 '19 at 07:55
  • @bagljas it's install inside virtualenv – PPShein Mar 12 '19 at 07:59
  • 1
    Can you please check output of following commands in the terminal in which you had `flask run` command working and `python ` not? `which python`, `which pip`, and `which flask`. They should all show paths to executables in your virtualenv e.g. `~/.virtualenvs/tmp-ec6bfce85be4e9c8/bin/python` – bagljas Mar 12 '19 at 08:06
  • Furthermore, for you app to work via command `python main.py`, you should add ` if \_\_name\_\_ == '\_\_main\_\_': \n app.run()` – bagljas Mar 12 '19 at 08:11
  • 1
    Will `pip uninstall flask && python -m pip install flask` do the trick? – Pavel Vergeev Mar 12 '19 at 08:12
  • @bagljas both are under my project directory `/Users/user/Documents/Projects/Python/flask/bin/...` – PPShein Mar 12 '19 at 08:25
  • @PavelVergeev that was working. Cool... – PPShein Mar 12 '19 at 08:29
  • @PPShein awesome! That means that probably command `pip` points to the wrong, system-wide, `pip`. In order to use the local pip do `python -m pip`. – Pavel Vergeev Mar 12 '19 at 08:38

4 Answers4

15

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.

Pavel Vergeev
  • 3,060
  • 1
  • 31
  • 39
0

As Pavel said; you might use python 2.7 instead of python 3 to run your project...

I had this issue and by using this command instead of Pavel's command, problem fixed

pip uninstall flask && python3.7 -m pip install flask

You can replace flask by any other lib and this will work

Hope it works for everyone sees this too

Milad Yarmohammadi
  • 1,253
  • 2
  • 22
  • 37
0

If you are using Visual Studio Code IDE with pipenv and getting the above error, you should check this link.

A_S
  • 127
  • 7
0

The previous answers are fine but you may need to be careful of the version of python you use to install flask this time. For me,

pip3 uninstall flask && python -m pip install flask

did not work, but

pip3 uninstall flask && python3 -m pip install flask

did work. The issue of having the right version when installing flask and its installation in the virtual environment also has consequences. Find out more in this article: ModuleNotFoundError.

N0blW8man
  • 1
  • 2