Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python flask import error

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

like image 653
Manarjan Singh Ghai Avatar asked Feb 09 '13 23:02

Manarjan Singh Ghai


People also ask

How do you fix an import error on a flask?

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 .

How do I fix No module named flask error?

The Python "ModuleNotFoundError: No module named 'flask'" occurs when we forget to install the Flask module before importing it or install it in an incorrect environment. To solve the error, install the module by running the pip install Flask command.


1 Answers

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

like image 112
Nishant Avatar answered Oct 05 '22 23:10

Nishant