Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot import flask from project directory but works everywhere else

Tags:

python

flask

So I have run into a funny problem when trying to use Flask, I can only run it from ~/ (home) and not from ~/Projects/projectfolder. I'm using Python 2.7.4 installed via their homepage, virtualenv and virtualenvwrapper. Every time it's the same:

$ mkvirtualenv project
New python executable in project/bin/python
Installing setuptools............done.
Installing pip...............done.

Then I install Flask:

$ pip install flask
[...]
Successfully installed flask Werkzeug Jinja2
Cleaning up...

Then I open Python from my home directory:

(project) $ python
>>> from flask import Flask
>>>

Then I quit and go to my project folder:

(project) $ cd ~/Projects/example
(project) $ python
>>> from flask import Flask
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
   File "flask.py", line 1, in <module>
     from flask import Flask
ImportError: cannot import name Flask

And I'm a bit lost as to why this is happening, anybody have any ideas?

like image 252
Sondre Nilsen Avatar asked May 04 '13 11:05

Sondre Nilsen


1 Answers

According to you traceback, you have a module of your own called flask.py in ~/Projects/example.

The current directory is searched before the actual package installation path, so it shadows the "real" Flask.

like image 142
icecrime Avatar answered Sep 29 '22 13:09

icecrime