50

I am running the following code

from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello():
    return "Hello World!"
if __name__ == "__main__":
    app.run(host='0.0.0.0', port=80, debug=True)

and getting the following error

Traceback (most recent call last):
  File "test.py", line 1, in <module>
    from flask import Flask
  File "/home/pi/programs/flask.py", line 1, in <module>
    from flask import Flask
ImportError: cannot import name Flask

I have tried installing flask through various methods , but still the problem persists

also, is there any alternative to flask???

11
  • Install it with pip install Flask. What operating system are you running?
    – Blender
    Feb 9, 2013 at 23:02
  • 1
    We will need more information about how you attempted to install flask. What, exactly are these "various methods" you attempted? What is your OS? etc. Feb 9, 2013 at 23:04
  • 1
    Is /home/pi/programs in either your pythonpath or your current working directory? Check it with echo "$PYTHONPATH" Feb 9, 2013 at 23:04
  • i am using debian wheezy on a raspberry pi Feb 9, 2013 at 23:17
  • 39
    I had this error too. Beware not to name file as flask.py!
    – zigomir
    Jul 31, 2013 at 17:17

9 Answers 9

179

I ran into this error because I named the test file as flask.py and tried to run it! It creates namespace conflict with the real flask module!

Delete the local test file that you named flask.py and the respective flask.pyc. Give some other name! This will happen with other modules like socket etc where you are likely to give the same name for the test file as the standard module :-)

12
  • 6
    YES, that's it. This cardinal mistake again...upvote.
    – Alex
    May 5, 2014 at 5:58
  • This is the mistake I made. Thanks! Jan 25, 2016 at 22:16
  • sqlalchemy, users also bump into the same issue. they name it as sqlalchemy.py :)
    – Nishant
    Apr 28, 2016 at 10:04
  • 2017... And this reply is still useful. Thank you and shame on my and on the filename I've chosen! May 28, 2017 at 17:12
  • 1
    As you point out, happens with socket too. lolz Feb 9, 2021 at 15:41
26

The reason is your python file name is flask.

2
  • 2
    Could you provided more information to solving the problem? Aug 10, 2016 at 9:36
  • Yikes! that was my problem too Mar 26, 2018 at 16:28
16

Just run apt-get install python3-flask

Edited to install the python3 version, since nobody should be using python2 now.

3
  • what ip should give in app.run(host='0.0.0.0', port=80, debug=True) i am getting the following error ** * Running on 0.0.0.0:80 Traceback (most recent call last): File "test.py", line 14, in <module> app.run(host='0.0.0.0', port=80, debug=True) run_simple(host, port, self, **options) File "/usr/local/lib/python2.7/dist-packages/werkzeug/serving.py", line 613, in run_simple test_socket.bind((hostname, port)) File "/usr/lib/python2.7/socket.py", line 224, in meth return getattr(self._sock,name)(*args) socket.error: [Errno 98] Address already in use ** Feb 10, 2013 at 0:15
  • 0.0.0.0 is fine, it will listen to all the interfaces. The port is not okay since you need to be root to use that. For testing usually people use 8080.
    – LtWorf
    Feb 10, 2013 at 0:16
  • 3
    run "apt-get install python3-flask" if you are running python v3
    – Gilad
    Feb 21, 2016 at 16:39
11

Just rename flask.py file also delete flask.pyc file

5

It's because of the name flask.py. It will import itself if the name is flask.py. Change the name and try again.

2
  • 1
    Could you be more specific about what the name "flask.py" is problematic and what it should be changed to?
    – vielkind
    Aug 16, 2018 at 15:56
  • 1
    @vealkind its because the file will try to import the itself. Write some print statement in flask.py and then import flask in the same file and it will print the same thing twice, so.. Aug 16, 2018 at 16:30
1

Restart virtual environment

$ virtualenv flask

Into dir flask run

$source ./bin/activate

Install python module again

$pip install "module"
1

I had the same issue. Apparently you can't name your file socket.py either.

0

exactly when we create file name as flask.py and then execute it for the first time it will execute and at the same time framework creates another file called flask.pyc .Now stop the process and start it again it will throw this error as instead of looking to actual framework flask file it is looking in to the one you created . To solve this problem Go to the folder you created flask.py and delete flask.pyc and then rename flask.py to some test_1.py ..save it and execute it . You should see no errors .

0

Add PYTHONPATH to your environment: export PYTHONPATH=/root/environments/my_env/lib/python3.6/site-packages/

Not the answer you're looking for? Browse other questions tagged or ask your own question.