python flask import error

107,552

Solution 1

Just run apt-get install python3-flask

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

Solution 2

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 :-)

Solution 3

The reason is your python file name is flask.

Solution 4

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

Solution 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.

Share:
107,552
Admin
Author by

Admin

Updated on December 16, 2020

Comments

  • Admin
    Admin over 3 years

    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???

  • Admin
    Admin about 11 years
    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 **
  • LtWorf
    LtWorf about 11 years
    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.
  • Alex
    Alex almost 10 years
    YES, that's it. This cardinal mistake again...upvote.
  • Dendi Suhubdy
    Dendi Suhubdy over 8 years
    This is the mistake I made. Thanks!
  • Gilad
    Gilad about 8 years
    run "apt-get install python3-flask" if you are running python v3
  • Nishant
    Nishant about 8 years
    sqlalchemy, users also bump into the same issue. they name it as sqlalchemy.py :)
  • Barry Michael Doyle
    Barry Michael Doyle over 7 years
    Could you provided more information to solving the problem?
  • Rainbowbreeze
    Rainbowbreeze almost 7 years
    2017... And this reply is still useful. Thank you and shame on my and on the filename I've chosen!
  • Tayyab Anwar
    Tayyab Anwar about 6 years
    Thank you so much! I was making the same mistake.
  • Víctor López
    Víctor López about 6 years
    That was the real problem. Thanks for sharing it.
  • Ken Aspeslagh
    Ken Aspeslagh about 6 years
    Yikes! that was my problem too
  • LukaszTaraszka
    LukaszTaraszka about 6 years
    I hate this kind of problems :/
  • gented
    gented almost 6 years
    Bloody hell, the amount of times I make this same error is unprecedented.
  • vielkind
    vielkind over 5 years
    Could you be more specific about what the name "flask.py" is problematic and what it should be changed to?
  • Admin
    Admin over 5 years
    @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..
  • Farley
    Farley about 5 years
    You'd think after programming in a language for years you would not forget this simple little gem. I think this is the third time I landed on this page over the years. Thanks again!
  • Mina Gabriel
    Mina Gabriel about 5 years
    :) OMG - I don't like when this happens
  • demongolem
    demongolem almost 5 years
    For me, it was because I had named a Python package as flask
  • interestedparty333
    interestedparty333 about 3 years
    As you point out, happens with socket too. lolz