198

How does one create a Python friendly environment in Xcode 4, 5, 6 or 7?

13
  • 1
    Komodo is a good Python specific alternative especially if you aren't trying to combine Python with any OSX specific stuff.
    – user177800
    Mar 14, 2011 at 17:42
  • Hmm. It looks interesting but I don't feel like coughing up the cash. Mar 14, 2011 at 17:45
  • Komodo Edit is their free option. I have IDE and if you do serious Python work it is well worth it.
    – user177800
    Mar 14, 2011 at 18:33
  • 1
    I'd recommend Wing 4 just for its code assistant ... very nice. But Xcode 4 is rather pretty :-).
    – NickZoic
    Mar 30, 2011 at 10:30
  • 1
    What the hell? A question for future? Dated Mar 11 '2011 at 18:40??? Jun 11, 2015 at 14:57

8 Answers 8

265
+100

I figured it out! The steps make it look like it will take more effort than it actually does.

These instructions are for creating a project from scratch. If you have existing Python scripts that you wish to include in this project, you will obviously need to slightly deviate from these instructions.

If you find that these instructions no longer work or are unclear due to changes in Xcode updates, please let me know. I will make the necessary corrections.

  1. Open Xcode. The instructions for either are the same.
  2. In the menu bar, click “File” → “New” → “New Project…”.
  3. Select “Other” in the left pane, then "External Build System" in the right page, and next click "Next".
  4. Enter the product name, organization name, or organization identifier.
  5. For the “Build Tool” field, type in /usr/local/bin/python3 for Python 3 or /usr/bin/python for Python 2 and then click “Next”. Note that this assumes you have the symbolic link (that is setup by default) that resolves to the Python executable. If you are unsure as to where your Python executables are, enter either of these commands into Terminal: which python3 and which python.
  6. Click “Next”.
  7. Choose where to save it and click “Create”.
  8. In the menu bar, click “File” → “New” → “New File…”.
  9. Select “Other” under “OS X”.
  10. Select “Empty” and click “Next”.
  11. Navigate to the project folder (it will not work, otherwise), enter the name of the Python file (including the “.py” extension), and click “Create”.
  12. In the menu bar, click “Product” → “Scheme” → “Edit Scheme…”.
  13. Click “Run” in the left pane.
  14. In the “Info” tab, click the “Executable” field and then click “Other…”.
  15. Navigate to the executable from Step 5. You might need to use ⇧⌘G to type in the directory if it is hidden.
  16. Select the executable and click "Choose".
  17. Uncheck “Debug executable”. If you skip this step, Xcode will try to debug the Python executable itself. I am unaware of a way to integrate an external debugging tool into Xcode.
  18. Click the “+” icon under “Arguments Passed On Launch”. You might have to expand that section by clicking on the triangle pointing to the right.
  19. Type in $(SRCROOT)/ (or $(SOURCE_ROOT)/) and then the name of the Python file you want to test. Remember, the Python program must be in the project folder. Otherwise, you will have to type out the full path (or relative path if it's in a subfolder of the project folder) here. If there are spaces anywhere in the full path, you must include quotation marks at the beginning and end of this.
  20. Click “Close”.

Note that if you open the "Utilities" panel, with the "Show the File inspector" tab active, the file type is automatically set to "Default - Python script". Feel free to look through all the file type options it has, to gain an idea as to what all it is capable of doing. The method above can be applied to any interpreted language. As of right now, I have yet to figure out exactly how to get it to work with Java; then again, I haven't done too much research. Surely there is some documentation floating around on the web about all of this.

Running without administrative privileges:

If you do not have administrative privileges or are not in the Developer group, you can still use Xcode for Python programming (but you still won't be able to develop in languages that require compiling). Instead of using the play button, in the menu bar, click "Product" → "Perform Action" → "Run Without Building" or simply use the keyboard shortcut ^⌘R.

Other Notes:

To change the text encoding, line endings, and/or indentation settings, open the "Utilities" panel and click "Show the File inspector" tab active. There, you will find these settings.

For more information about Xcode's build settings, there is no better source than this. I'd be interested in hearing from somebody who got this to work with unsupported compiled languages. This process should work for any other interpreted language. Just be sure to change Step 5 and Step 16 accordingly.

23
  • 2
    Awesome that you figured this out. is interactive debugging possible? I'd be curious to know for the few times that I have to do it.
    – Tom Willis
    Mar 29, 2011 at 19:15
  • 2
    Xcode4 often does some strange auto-indentation for me. Did you have similar problems? Have you come around them somehow?
    – Albert
    Mar 30, 2011 at 12:08
  • 1
    @lciamp, I have no experience with pyCharm; however, I have gotten this to work on versions of Python that did not come pre-installed on OS X. Jun 23, 2012 at 6:05
  • 2
    @xxmbabanexx, the instructions were slightly out of date. They changed the text of the label for that field. I have updated the instructions accordingly. Mar 28, 2013 at 13:41
  • 1
    I tried this in Xcode 5.02. It worked except for 2 things: 1) I couldn't get the scheme to work with $(SOURCE_ROOT) so I gave up and used a hardwired path; 2) when I run the python script Xcode stops at dyld`_dyld_start: If I "continue" it seems to work. Anyone else experience this?
    – mts
    Nov 21, 2013 at 21:02
29

I've created Xcode 4 templates to simplify the steps provided by Tyler.
The result is Python Project Template for Xcode 4.

Now what you need to do is download the templates, move it to /Developer/Library/Xcode/Templates/Project Templates/Mac/Others/ and then new a Python project with Xcode 4.

It still needs manual Scheme setup (you can refer to steps 12–20 provided by Tyler.)

1
  • I tried making a template to no avail. I don't know too much about it so I didn't get it completed. Props for the templates. Surely there's a way to automate the scheme setup. :/ Dec 27, 2011 at 20:39
13

Procedure to get Python Working in XCode 7

Step 1: Setup your Project with a External Build System

enter image description here

Step 1.1: Edit the Project Scheme

enter image description here

Step 2: Specify Python as the executable for the project (shift-command-g) the path should be /usr/bin/python

enter image description here

Step 3: Specify your custom working directory

enter image description here

Step 4: Specify your command line arguments to be the name of your python file. (in this example "test.py")

enter image description here

enter image description here

Step 5: Thankfully thats it!

(debugging can't be added until OSX supports a python debugger?)

3
  • This process seems to be missing some steps, starting right after step 1, for Xcode 7.0.1 (7A1001)... Can you clarify? Specifically, I'm not able to find the dialog shown in Step 2, and shift-command-g is the "Find Previous" command...
    – PTTHomps
    Oct 1, 2015 at 16:25
  • @TraxusIV the step that is missing is: in the main Xcode window, click on the project name. You should get a menu with "Edit Scheme...", "New Scheme.." and "Manage Schemes..." Select "Edit Scheme..." and you will get to the required window, in step 2 above.
    – Deesbek
    May 18, 2016 at 16:27
  • For the External Build System Build Tool, use the which command in Terminal to get the exact location for the Build Tool. "which lua" for instance returns "/usr/local/bin/lua" on my system.
    – C1pher
    Feb 27, 2017 at 22:34
8

You should try PyDev plug in for Eclipse. I tried alot of editors/IDE's to use with python, but the only one i liked the most is the PyDev plugin for Eclipse. It has code completion, debugger and many other nice features. Plus both are free.

3
  • That's what I am trying to do now. I haven't gotten it to work yet cause it seems like you have to jump through a couple of hoops to do so but that's looking like what I'll be doing. Mar 16, 2011 at 19:14
  • Yeah, once you add the Python interpreter your good to go. If you have any questions feel free to ask. Mar 17, 2011 at 1:59
  • 1
    How do you add Python interpreter to do interactive coding here? Jun 6, 2014 at 23:38
6

Another way, which I've been using for awhile in XCode3:

See steps 1-15 above.

  1. Choose /bin/bash as your executable
  2. For the "Debugger" field, select "None".
  3. In the "Arguments" tab, click the "Base Expansions On" field and select the target you created earlier.
  4. Click the "+" icon under "Arguments Passed On Launch". You may have to expand that section by clicking on the triangle pointing to the right.
  5. Type in "-l". This will tell bash to use your login environment (PYTHONPATH, etc..)
  6. Do step #19 again.
  7. Type in "-c '$(SOURCE_ROOT)/.py'"
  8. Click "OK".
  9. Start coding.

The nice thing about this way is it will use the same environment to develop in that you would use to run in outside of XCode (as setup from your bash .profile).

It's also generic enough to let you develop/run any type of file, not just python.

2

This Technical Note TN2328 from Apple Developer Library helped me a lot about Changes To Embedding Python Using Xcode 5.0.

0
2

This thread is old, but to chime in for Xcode Version 8.3.3, Tyler Crompton's method in the accepted answer still works (some of the names are very slightly different, but not enough to matter).

2 points where I struggled slightly:

Step 16: If the python executable you want is greyed out, right click it and select quick look. Then close the quick look window, and it should now be selectable.

Step 19: If this isn’t working for you, you can enter the name of just the python file in the Arguments tab, and then enter the project root directory explicitly in the Options tab under Working Directory--check the “Use custom working directory” box, and type in your project root directory in the field below it.

1
  • That trick with the left-click and 'quick-look' is great! helped me get it work! in Xcode 9.2 many thanks!
    – novski
    Jan 9, 2018 at 20:43
1

Try Editra It's free, has a lot of cool features and plug-ins, it runs on most platforms, and it is written in Python. I use it for all my non-XCode development at home and on Windows/Linux at work.

Not the answer you're looking for? Browse other questions tagged or ask your own question.