Using Python 3.1 with TextMate

eozzy picture eozzy · Nov 21, 2009 · Viewed 16.2k times · Source

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.

Thanks

Answer

Ned Deily picture Ned Deily · Nov 21, 2009

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. Open a new or existing TextMate Project (File -> New Project or File -> Open)

  2. De-select any file in the project list sidebar.

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

  4. Click + to add a new variable.

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

  6. Close the Information window and save the Project (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.

To change the Python version used globally within TextMate:

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

  2. click on the Advanced pane

  3. click on the Shell Variable tab

  4. click the + to add a new variable

  5. enter TM_PYTHON in the Variable field and the full path to the python in the Value field (perhaps /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.

UPDATE (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 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)

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

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