Add Python Project Template to Xcode 4 Step by Step

by Chen-Hai Teng

Purpose

To create/edit python project easier.

Introduction

In Xcode 4, the support of python is removed. User who want to edit and run python by Xcode 4 need to configure the project himself. Besides, with the difference of template format between Xcode 3 and Xcode 4, it is hard to create a custom template for python, and the configuration process becomes boring routine. This guide try to help the python user to built a simple python project template to shorten the process.

This guide will not explain the new template format of Xcode, if you are interesting in building your own template, A minimal project template for Xcode 4 would be help.

Create Python Project Template

1. Create a new project group for Python

In Xcode 4, the templates for Mac are located in following directory:

/Developer/Library/Xcode/Templates/Project Templates/Mac/

There are several sub-folders under Mac. Each folder corresponds to a group in “New Project...” sheet in Xcode4. To create a new group, just create a new folder named “Python” in it.

2. Duplicate exist template -- it would be our base-template

Now, go to /Developer/Library/Xcode/Templates/Project Templates/Mac/Others/, copy “External Build System.xctemplate” into out new template group folder, and rename it to “Python.xctemplate”

3. Modifies TemplateInfo.plist

Open TemplateInfo.plist under Python.xctemplate (I would like to use Xcode to edit it.)

Change the “Identifier” field. The new identifier should be different from exist one. Then, expands “Targets”, and finds the key “BuildToolPath”. Changes the value to “/usr/bin/python” (For python 3, it might be /usr/local/bin/python3 .) Finally, changes the “Description” to what you prefer.

Before next step, try to create a new project in Xcode 4, you should see a dialog like following:

Click “Next” button, and you will see:

Now, you have a template to specify python interpreter as your external build system.

Create Python File Template

Although we have a python project, it is empty. You can add a python file yourself, but it would be more convenient to generate a default file while new a project.

1. Prepare an python template. I prefer the following format:

"""

" ___FILENAME___

“ ___PROJECTNAME___

" Created by ___FULLUSERNAME___ on ___DATE___.

“ Copyright ___YEAR___ ___ORGANIZATIONNAME___. All rights reserved.

"""

# write your python code below

Save it as “___PROJECTNAME___.py” in Python.xctemplate.

2. Add following code into TemplateInfo.plist

<key>Definitions</key>

<dict>

<key>___PROJECTNAME___.py</key>

<dict>

<key>Path</key>

<string>___PROJECTNAME___.py</string>

</dict>

</dict>

<key>Nodes</key>

<array>

<string>___PROJECTNAME___.py</string>

</array>

Now, try to create a new Python project again. The project should look like:

Configure Scheme to Run Python

Before writing python code, it is necessary to configure the scheme. Unfortunately, there is no information about how to configure scheme in template format. We need to set it manually.

1. In the menu bar, click "Product" → "Edit Scheme…" (or press ⌘ <) .

2. Select "Run" in the sidebar.

3. In the "Info" tab, click the "Executable" field and then click "Other…".

4. Choose the python interpreter in your system. It should be located as /usr/local/bin/python3 for python 3, and /usr/bin/python for built-in python 2. You may need to use ⇧⌘G to type in the directory if it is hidden.

5. For the "Debugger" field, select "None".

6. Switch to "Arguments" tab, set "Base Expansions On" field to your target.

7. Add a new item in "Arguments Passed On Launch" by click the "+" button below.

8. Type in $(SOURCE_ROOT)/ and then the name of the Python file you want to test. For example: $(SOURCE_ROOT)/MyPython/MyPython.py.

9. Click "OK".

Now, you can write your python code, and run it with Xcode 4.

You can download my final results at  Python Project Template for Xcode 4.