python flask import error
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.
Admin
Updated on December 16, 2020Comments
-
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 about 11 yearswhat 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 about 11 years0.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 almost 10 yearsYES, that's it. This cardinal mistake again...upvote.
-
Dendi Suhubdy over 8 yearsThis is the mistake I made. Thanks!
-
Gilad about 8 yearsrun "apt-get install python3-flask" if you are running python v3
-
Nishant about 8 yearssqlalchemy, users also bump into the same issue. they name it as sqlalchemy.py :)
-
Barry Michael Doyle over 7 yearsCould you provided more information to solving the problem?
-
Rainbowbreeze almost 7 years2017... And this reply is still useful. Thank you and shame on my and on the filename I've chosen!
-
Tayyab Anwar about 6 yearsThank you so much! I was making the same mistake.
-
Víctor López about 6 yearsThat was the real problem. Thanks for sharing it.
-
Ken Aspeslagh about 6 yearsYikes! that was my problem too
-
LukaszTaraszka about 6 yearsI hate this kind of problems :/
-
gented almost 6 yearsBloody hell, the amount of times I make this same error is unprecedented.
-
vielkind over 5 yearsCould you be more specific about what the name "flask.py" is problematic and what it should be changed to?
-
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 about 5 yearsYou'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 about 5 years:) OMG - I don't like when this happens
-
demongolem almost 5 yearsFor me, it was because I had named a Python package as flask
-
interestedparty333 about 3 yearsAs you point out, happens with socket too. lolz