from Flask import Flask ImportError: No module named Flask
Asked Answered
A

6

7

I am following the tutorial here.

My file looks like this:

from flask import Flask
app = Flask(__name__)

@app.route("/")
def main():
    return "Welcome!"

if __name__ == "__main__":
    app.run()

I run python app.py and get the following:

Traceback (most recent call last):
File "app.py", line 1, in <module>
from flask import Flask
ImportError: No module named Flask

I do have flask installed. I was thinking it's a $PATH issue. I don't really know where to begin as far as troubleshooting goes.

which flask gives me: /usr/local/bin/flask

which python gives me: /usr/bin/python

Any help is much appreciated, there are other similar issues out there but those solutions have not helped. Happy to answer any questions. Thank you.

Answers to questions:

Q. Which Python version? A. Python 2.7.10

Q. How did you install Flask? A. pip install flask

Ava answered 12/10, 2016 at 2:43 Comment(2)
It sounds like you have a package named flask next to app.py.Vlaminck
If pip installation didn't fail, all paths should be correct. You could check $PYTHONPATH. @dim has a good point. Do you have a folder named flask or a file you named flask.py in the same directory?Anyway
K
3

before run your app.py you need to activate your server by this command

. venv/bin/activate
Kornher answered 15/11, 2017 at 11:37 Comment(0)
T
2

It looks like that you are using linux, if you are using ubuntu try: (similar for other linux distributions)

sudo apt-get install python-flask

It helped me install many packages that pip fails to install.

Tartu answered 14/2, 2018 at 21:16 Comment(0)
A
1

The app package and run.py file need to be in the same directory level.

app\
    templates\
    __init__.py
venv\
run.py

Above folder structure should fix.

P.S. pip install all necessary packages into virtual enviroment (venv in my example)

Amaleta answered 2/8, 2017 at 10:5 Comment(0)
A
0

You can try to uninstall and to reinstall it again(you can follow this link for more detaile) :

http://flask.pocoo.org/docs/0.12/installation/#installation

You can take anaconda and there you can make enviroment for every version of python .Anaconda is getting very easy from this link: https://docs.anaconda.com/anaconda/install/

Then you can open terminal and pip install flask (if you are on windows) or conda pip install flask .

I recommand to use python3.5 not python 2.7 .

Aphorize answered 14/2, 2018 at 22:50 Comment(0)
M
0

if you are on Python 3, try installing flask with

pip3 install flask

and if you fail (you are likely to get an command prompt error as below)

'pip3' is not recognized as an internal or external command, operable program or batch file.

try

py -3 -mpip install flask on windows

this command installs flask module on python 3, and then you could run your script. worked for me!

Monarchism answered 10/6, 2020 at 2:28 Comment(0)
S
-2

Python 3 :

pip3 install flask

Python 2 :

pip install flask
Slam answered 17/9, 2019 at 8:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.