2

I'm following a tutorial to learn to use Flask. I'm using Ubuntu 18.04.2 lts and python3. Everything is newly installed and fully updated. Here is the entirety of my code:

from flask import Flask

app = Flask(__name__)

@app.route('/')
def hello_world():
    return 'Hello World'


if __name__ == '__main__':
    app.run()

This is saved as hello.py and when I try to run it from the terminal using "python3 hello.py" I get this error:

Traceback (most recent call last):
  File "hello.py", line 1, in <module>
    from flask import Flask
  File "/home/wyattsullivan/Documents/flask.py", line 3, in <module>
    app = flask.Flask(__name__)
AttributeError: module 'flask' has no attribute 'Flask'

Why am I getting this error and how can I fix it?

  • 1
    Looks like you have a file named flask.py locally which is imported instead of the Flask module. You could rename it to something else and try. – aadibajpai Aug 08 '19 at 17:52

1 Answers1

7

You have a file named flask.py in the same directory as your hello.py. That file overshadows the flask module since it is locally available. Hence in hello.py,

from flask import Flask is interpreted as from the flask.py file import Flask which doesn't exist in that file.

To fix, rename that flask.py to something else.

aadibajpai
  • 356
  • 3
  • 16