2

I tried this minimal example:

from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello_world():
    return 'Hello World!'

if __name__ == '__main__':
    app.run(debug = True)

When I try python hello.py, everything goes well. However, when I try to run it from Textmate (Shift + Cmd + R) an error is thrown:

Traceback (most recent call last):
  File "/Users/user/EventFeed/hello.py", line 1, in <module>
    from flask import Flask
ImportError: No module named flask

Textmate calls pythonw instead of python. When I try pythonw myself the same error is thrown.

The man pythonw states that As of Python 2.5, python and pythonw are interchangeable though they appear not to be in this case.

Would you have an idea of what happens?

(Question Code that works with python and not with pythonw does not answer the question despite its similar title.)

Community
  • 1
  • 1
Thibaud
  • 89
  • 8
  • 1
    Are you sure your `python` and `pythonw` are from the same Python installation? From the command line, run each one, then `import sys; print sys.executable, sys.path` and see what they say. – abarnert Sep 25 '14 at 23:34
  • 1
    Wait a sec, this is a Mac, not Windows… just `which python` and `which pythonw` on the bash prompt will do it. And I'm 90% sure I already know what you're going to see, too. – abarnert Sep 25 '14 at 23:35

1 Answers1

2

The problem is that your pythonw and your python are not pointing at the same Python installations.

Why?

Most likely because you've installed a second Python 2.7 that doesn't include the obsolete pythonw, but Apple's pre-installed Python 2.7 definitely does include it.

The quickest way to check this is the which command. For example, on one of my machines:

$ which python
/usr/local/bin/python
$ which pythonw
/usr/bin/pythonw

That first one is a symlink to a Homebrew install of Python 2.7, while the second is Apple's Python 2.7. Your exact details may differ; the first one may be a symlink to /Library/Frameworks/Python.framework/Versions/2.7/bin/python, or a wrapper executable that actually lives in /usr/local/bin, or it may be in /opt/local, etc. The point is that they're not in the same directories.

At any rate, your two separate installations of Python don't share the same site-packages (and they shouldn't), so the fact that you've installed Flask for the second one doesn't help the Apple one. You can verify this by running them and printing out sys.path:

$ python
>>> import sys
>>> sys.path
['', '/usr/local/lib/python2.7/site-packages', …]
>>> ^D
$ pythonw
>>> import sys
>>> sys.path
['', '/Library/Python/2.7/site-packages', …]
>>> ^D

Anyway, the simplest solution is to configure your editor to run python instead of pythonw—or, better, give it an absolute path to a Python interpreter like /usr/local/bin/python2.7 to make absolutely sure you know what you're running.

(I don't know TextMate very well, but from this source it looks like it has a setting named TM_PYTHON that should control this…)

abarnert
  • 354,177
  • 51
  • 601
  • 671
  • 1
    You're right. `python` was linking to a virtualenv `python` which was himself linking to the Homebrew `python` whereas `pythonw` was directly linking to the Homebrew `pythonw`. The reason is `pythonw` is not included in a directory when `virtualenv venv` and the purpose of virtualenv is precisely to isolate the packages even if the executables link to the same endpoint. – Thibaud Sep 26 '14 at 02:25
  • @Thibaud: Ah, I didn't think of virtualenv, because I've never set up an IDE to run with a virtualenv, but I can see how that would cause the problem. So, the details in my answer aren't actually correct; do you think I should edit it? – abarnert Sep 26 '14 at 17:52
  • I did not mention virtualenv in my question, so your answer is perfectly fine because general enough to apply in both cases while precise enough to get me catch the problem. – Thibaud Sep 26 '14 at 21:35