0

I tried the following simple code,

from flask import Flask
app = Flask(__name__)

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

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

it is running fine with,

python hello.py

but it gives an error when i try with python3

ImportError: cannot import name 'Flask'
0

1 Answer 1

7

A package is installed against a specific Python version/location. Installing Flask for Python 2 (which is probably what the python and pip commands are aliased to), doesn't install it for Python 3.

You should really just use virtualenv to control exactly what versions and packages you are using.

This creates a Python 3 environment and installs Flask:

virtualenv -p /usr/bin/python3 my_py3_env
source my_py3_env/bin/activate
pip install flask

When you open a new terminal, just source the activate script again to keep using the environment.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.