本文介绍了将Python 3.1与TextMate结合使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

TextMate似乎使用了我假定的内置Python版本(sys.path不起作用).您如何配置它以改为使用3.1?我已经安装了3.1软件包,可以使用IDLE进行交互式会话,但是现在需要使用TextMate.

TextMate seems to use the built-in Python version I assume (sys.path doesn't work). How do you configure it to use 3.1 instead? I've already installed the 3.1 package and I can use IDLE for interactive sessions, but I need to use TextMate now.

谢谢

推荐答案

TextMate使用TM_PYTHON变量的值来查找Python解释器的路径.一个很好的解决方案是利用TextMate的功能在每个项目的基础上定义诸如TM_PYTHON之类的变量:

TextMate uses the value of the TM_PYTHON variable to find the path to the Python interpreter. A good solution is to take advantage of TextMate's ability to define variables like TM_PYTHON on a per-project basis:

  1. 打开一个新的或现有的TextMate项目(File -> New ProjectFile -> Open)

在项目列表侧边栏中取消选择任何文件.

De-select any file in the project list sidebar.

单击侧栏中的Get Info( i )图标.出现Project Information窗格.

Click on the Get Info (i) icon in the sidebar. A Project Information pane appears.

单击+添加新变量.

在变量"字段中输入TM_PYTHON,然后在值"字段中输入所需python的完整路径(例如,/usr/local/bin/python3.1).

Enter TM_PYTHON in the Variable field and the full path to the desired python in the Value field (for example, /usr/local/bin/python3.1).

关闭信息"窗口并保存项目(File-> Save Project As).

Close the Information window and save the Project (File -> Save Project As).

然后,您可以根据需要向项目添加文件,这些文件将通过TextMate Python捆绑包的运行脚本"命令在选定的python下运行.例如,您可能想要保存一个Python 3项目,以便在Python 3下运行即席脚本.对于较大的项目,无论如何,您都想为其创建一个单独的TextMate项目.

Then you can add files as needed to the project and they will be run under the chosen python with TextMate Python bundle's Run Script command. You might want to save a Python 3 project, say, for running ad-hoc scripts under Python 3. For bigger projects, you'll want to create a separate TextMate project for it anyway.

要更改TextMate中全局使用的Python版本:

To change the Python version used globally within TextMate:

  1. TextMate菜单栏中,打开TextMate-> Preferences

  1. From the TextMate menu bar, open TextMate -> Preferences

单击Advanced窗格

单击Shell Variable标签

单击+添加新变量

TM_PYTHON,在Value字段中输入python的完整路径(可能是/usr/local/bin/python3.1)

enter TM_PYTHON in the Variable field and the full path to the python in the Value field (perhaps /usr/local/bin/python3.1)

正如Alex指出的那样,您可以通过全局更改Python版本来破坏其他TextMate功能,因此按项目进行更改可能是更好的解决方案.

As Alex points out, you may break other TextMate functionality by changing the Python version globally so the per-project change is probably a better solution.

更新(2010-10-31):

UPDATE (2010-10-31):

还有另一种方法可能对于某些项目更易于使用. TextMate的Python软件包中的Run命令似乎尊重 shebang 行中正在运行的文件.因此,无需修改TM_PYTHON,您可以通过包含如下第一行来指定要使用的解释器的路径:

There is another approach that may be easier to use for some projects. The Run command in TextMate's Python bundle appears to respect a shebang line in the file being run. So, instead of modifying TM_PYTHON, you can specify the path to the interpreter to be used by including a first line like this:

#!/usr/local/bin/python3.1

# sample code to show version
import sys
print(sys.version_info)

在许多情况下,您宁愿不使用 hardwire 绝对路径,而是通过普通的shell PATH环境变量来管理使用.传统上,/usr/bin/env用于此目的.但是,当在TextMate下运行时,通常不会使用您的外壳配置文件,因此对PATH的任何更改都不会在其中显示,包括/usr/local/bin/opt/local/bin或所需的python3命令所在的位置.为了解决这个问题,您可以将全局PATH shell变量添加或修改为TextMate-> Preferences(参见上文),其值例如为/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin.然后,您可以使用更通用的shebang行,如下所示:

In many case you would prefer not to hardwire the absolute path but manage use through the normal shell PATH environment variable. Traditionally /usr/bin/env is used for that purpose. However when running under TextMate, your shell profile files are not normally used so any changes to PATH do not show up there including possibly /usr/local/bin or /opt/local/bin or wherever your desired python3 command is located. To get around that, you can add or modify a global PATH shell variable to TextMate -> Preferences (see above) with a value of, say, /usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin. Then you can use a more general shebang line like this:

#!/usr/bin/env python3

(这一切似乎都适用于最新的香草TextMate及其Python捆绑包:不能保证与较早版本或其他Python捆绑包有关.)

(This all seems to work with the most recent vanilla TextMate and its Python bundle: no guarantees about earlier versions or with other Python bundles.)

这篇关于将Python 3.1与TextMate结合使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-07 01:08