python flask import error

Question:

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

Answers:

Just run apt-get install python3-flask

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

Answered By: LtWorf

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 🙂

Answered By: Nishant

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

Answered By: hardik k

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 .

Answered By: Vijay

The reason is your python file name is flask.

Answered By: user6699563

Restart virtual environment

$ virtualenv flask

Into dir flask run

$source ./bin/activate

Install python module again

$pip install "module"
Answered By: Jose Angel Lopez

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

Answered By: Chase Roberts

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

Answered By: user9200165

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

Answered By: Carlos Oliveira

go to your vs code terminal then type this command

  1. sudo apt install python3-venv

  2. python3 -m venv my-project-env

  3. source my-project-env/bin/activate

  4. pip install flask

  5. after install CTRL+SHIFT+P and search for Python Interpreter
    enter image description here

  6. Select Your virtual Environment my-project-env which has created in above
    enter image description here

  7. now check the output. problem will be solved like this.
    enter image description here

Answered By: Saroar Zahan Sojib
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.