interpeter用python还是pythonw_当使用“ pythonw”时找不到模块。 (而不是“ python”)来运行应用程序...

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

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.)

解决方案

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…)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值