Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

Elias Sersun Uhalde
Elias Sersun Uhalde
12,131 Points

Can't import Flask

Any idea why does this throw an "importError: cannot import name: 'Flask'?

from flask import Flask
from flask import render_template

app = Flask(__name__)


app.run(debug = True)

3 Answers

Ok I figured it out. You canโ€™t call the file flask.py because then its trying to import the name Flask from itself. Try naming the file app.py or something other than flask and it will work. You have to be careful in naming your files so that they donโ€™t conflict with other packages.

By the way, you donโ€™t have any routes, Iโ€™m not sure if that was what youโ€™d wanted

You have to install flask :) Try running pip install flask in your terminal. If that doesnโ€™t work, try sudo pip install flask

Elias Sersun Uhalde
Elias Sersun Uhalde
12,131 Points

I'm using the workspaces, isn't flask installed already?

I would think so... did that work?

Elias Sersun Uhalde
Elias Sersun Uhalde
12,131 Points

I get back this:

treehouse:~/workspace$ pip install flask                                                                        
Requirement already satisfied (use --upgrade to upgrade): flask in /usr/local/pyenv/versions/3.5.0/lib/python3.5
/site-packages                                                                                                  
Requirement already satisfied (use --upgrade to upgrade): Werkzeug>=0.7 in /usr/local/pyenv/versions/3.5.0/lib/p
ython3.5/site-packages (from flask)                                                                             
Requirement already satisfied (use --upgrade to upgrade): Jinja2>=2.4 in /usr/local/pyenv/versions/3.5.0/lib/pyt
hon3.5/site-packages (from flask)                                                                               
Requirement already satisfied (use --upgrade to upgrade): itsdangerous>=0.21 in /usr/local/pyenv/versions/3.5.0/
lib/python3.5/site-packages (from flask)                                                                        
Requirement already satisfied (use --upgrade to upgrade): markupsafe in /usr/local/pyenv/versions/3.5.0/lib/pyth
on3.5/site-packages (from Jinja2>=2.4->flask)                                                                   
You are using pip version 7.1.2, however version 9.0.1 is available.                                            
You should consider upgrading via the 'pip install --upgrade pip' command.                                      
pyenv: cannot rehash: /usr/local/pyenv/shims isn't writable 

and when I try to run the file I get the same error:

treehouse:~/workspace$ python flask.py                                                                          
Traceback (most recent call last):                                                                              
  File "flask.py", line 1, in <module>                                                                          
    from flask import Flask                                                                                     
  File "/home/treehouse/workspace/flask.py", line 1, in <module>                                                
    from flask import Flask                                                                                     
ImportError: cannot import name 'Flask' 

See the answer, you need to run

sudo pip install flask
Elias Sersun Uhalde
Elias Sersun Uhalde
12,131 Points

Not working either...

treehouse:~/workspace$ sudo pip install flask                                                                   
bash: sudo: command not found

Thatโ€™s strangeโ€ฆ can you send me a snapshot of the workspace?

Elias Sersun Uhalde
Elias Sersun Uhalde
12,131 Points

Oookey, i'm an idiot. Thank you so much for your help!

Elias Sersun Uhalde
Elias Sersun Uhalde
12,131 Points

https://w.trhou.se/pg978fvwuw

Also thanks for all the help you've given me this past days.

No problem! Could you mark my answer as correct?