macos 将 Python 3.1 与 TextMate 结合使用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1775954/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using Python 3.1 with TextMate
提问by eozzy
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 似乎使用了我假设的内置 Python 版本(sys.path 不起作用)。你如何配置它以使用 3.1 代替?我已经安装了 3.1 包,我可以使用 IDLE 进行交互式会话,但我现在需要使用 TextMate。
Thanks
谢谢
回答by Ned Deily
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:
TextMate 使用TM_PYTHON
变量的值来查找 Python 解释器的路径。一个好的解决方案是利用 TextMate 的能力来定义变量,例如TM_PYTHON
在每个项目的基础上:
Open a new or existing TextMate Project (
File -> New Project
orFile -> Open
)De-select any file in the project list sidebar.
Click on the
Get Info
(i) icon in the sidebar. AProject Information
pane appears.Click + to add a new variable.
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
).Close the Information window and save the Project (
File
->Save Project As
).
打开一个新的或现有的 TextMate 项目(
File -> New Project
或File -> Open
)取消选择项目列表侧栏中的任何文件。
单击侧栏中的
Get Info
( i) 图标。Project Information
出现一个窗格。单击 + 添加新变量。
在变量字段中输入 TM_PYTHON,在值字段中输入所需 python 的完整路径(例如,
/usr/local/bin/python3.1
)。关闭信息窗口并保存项目 (
File
->Save Project As
)。
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 包的运行脚本命令在选定的 python 下运行。您可能想要保存一个Python 3
项目,例如,用于在 Python 3 下运行临时脚本。对于更大的项目,您无论如何都需要为其创建一个单独的 TextMate 项目。
To change the Python version used globally within TextMate
:
要更改在 内全局使用的 Python 版本TextMate
:
From the
TextMate
menu bar, openTextMate
->Preferences
click on the
Advanced
paneclick on the
Shell Variable
tabclick the
+
to add a new variableenter
TM_PYTHON
in theVariable
field and the full path to the python in theValue
field (perhaps/usr/local/bin/python3.1
)
从
TextMate
菜单栏中,打开TextMate
->Preferences
单击
Advanced
窗格单击
Shell Variable
选项卡单击
+
以添加新变量TM_PYTHON
在Variable
字段中输入以及在该字段中输入python 的完整路径Value
(可能/usr/local/bin/python3.1
)
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.
正如 Alex 指出的那样,您可能会通过全局更改 Python 版本来破坏其他 TextMate 功能,因此每个项目的更改可能是更好的解决方案。
UPDATE (2010-10-31):
更新 (2010-10-31):
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 shebangline 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:
对于某些项目,还有另一种方法可能更容易使用。的 Python 包中的Run
命令TextMate
似乎尊重正在运行的文件中的shebang行。因此,TM_PYTHON
您可以通过包含如下第一行来指定要使用的解释器的路径,而不是修改:
#!/usr/local/bin/python3.1
# sample code to show version
import sys
print(sys.version_info)
In many case you would prefer not to hardwirethe 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:
在许多情况下,您不希望硬连线绝对路径,而是通过正常的 shellPATH
环境变量管理使用。传统/usr/bin/env
上用于该目的。但是在运行的时候TextMate
,你的壳轮廓文件通常不使用这么路径的任何变化不会出现有可能包括/usr/local/bin
或/opt/local/bin
或任何你想要的python3
命令的位置。为了解决这个问题,您可以将全局PATH
shell 变量添加或修改为TextMate
-> Preferences
(见上文),其值为/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin
. 然后你可以使用更通用的shebang行,如下所示:
#!/usr/bin/env python3
(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.)
(这一切似乎都适用于最新的 vanillaTextMate
及其 Python 包:不保证早期版本或其他 Python 包。)
回答by Simon Banyard
Late to the party, sorry! I take it you want to run the script using TextMate's 'built-in' interpreter? I've found the simplest solution is to add a shebang, which works extremely well;
聚会迟到了,抱歉!我认为您想使用 TextMate 的“内置”解释器运行脚本?我发现最简单的解决方案是添加一个shebang,效果非常好;
#!/usr/bin/env python3
for python 3.1 or;
对于 python 3.1 或;
#!/usr/bin/env python
for default system python, although the latter is superfluous for the exercise.
对于默认系统 python,虽然后者对于练习来说是多余的。
回答by Alex Martelli
According to this long thread(which was about Python 3.0, and the TextMate version existing back last spring, but I believe is still valid for Python 3.1 and today's TextMate), you can get it done (e.g. via @Ned's answer), but once you do many TextMate commands may well break (because they're written to use Python 2, and Python 3 is not backwards compatible with Python 2 -- for example, the use of reload
, which disappeared in Python 3, is repeatedly mentioned in the thread). Still, it might work if you do not use or need much of TextMate's functionality (LaTeX typesetting for example was mentioned as something that totally breaks once you make TextMate use Python 3 instead of Python 2).
根据这个长线程(关于 Python 3.0,以及去年春天存在的 TextMate 版本,但我相信对 Python 3.1 和今天的 TextMate 仍然有效),你可以完成它(例如通过@Ned 的回答),但是一旦您执行的许多 TextMate 命令可能会中断(因为它们是为使用 Python 2 编写的,而 Python 3 与 Python 2 不向后兼容——例如,reload
在 Python 3 中消失的的使用在线程中反复提及)。尽管如此,如果您不使用或不需要 TextMate 的很多功能,它可能会工作(例如,LaTeX 排版被提及为一旦您让 TextMate 使用 Python 3 而不是 Python 2 就完全中断的东西)。
回答by philberndt
the shebang is the best solution, to see where python 3 is installed type in terminal:
shebang 是最好的解决方案,要查看 python 3 的安装位置,请在终端中键入:
which python3
哪个python3
you will get something like this:
你会得到这样的东西:
/usr/local/bin/python3
/usr/local/bin/python3
if nothing shows up first install python3
如果没有显示首先安装python3
and at the top of your script insert:
并在脚本的顶部插入:
#!/usr/local/bin/python3
#!/usr/local/bin/python3