navodissa
navodissa

Reputation: 55

python3 : Cannot import name flask

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'

Upvotes: 0

Views: 10088

Answers (1)

davidism
davidism

Reputation: 127180

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.

Upvotes: 7

Related Questions