Flask ImportError: No Module Named Flask

I'm following the Flask tutorial here:

http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-i-hello-world

I get to the point where I try ./run.py and I get:

    Traceback (most recent call last):
      File "./run.py", line 3, in <module>
        from app import app
      File "/Users/benjaminclayman/Desktop/microblog/app/__init__.py", line 1, in <module>
        from flask import Flask
    ImportError: No module named flask

This looks similar to:

ImportError: No module named flask

But their solutions aren't helpful. For reference, I do have a folder named flask which one user mentioned may cause issues.

try deleting the virtualenv you created. create a new virtualenv

    virtualenv flask

then

    cd flask

let's activate the virtualenv

    source bin/activate

now you should see (flask) on the left of the command line. Let's install flask

    pip install flask

Then create a file hello.py

    from flask import Flask
    app = Flask(__name__)

    @app.route("/")
    def hello():
        return "Hello World!"

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

and run it with

    python hello.py

UPDATE Flask 1.0.2

With the new flask release there is no need to run the app from your script. hello.py should look like this now:

    from flask import Flask
    app = Flask(__name__)

    @app.route("/")
    def hello():
        return "Hello World!"

and run it with

    FLASK_APP=hello.py flask run

make sure to be inside the folder where hello.py is when running the latest command.

All the steps before the creation of the hello.py apply for this case as well

From: stackoverflow.com/q/31252791

Back to homepage or read more recommendations: