198

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

Matt
  • 70,063
  • 26
  • 142
  • 172
Tyler Crompton
  • 11,740
  • 12
  • 59
  • 91
  • 1
    Komodo is a good Python specific alternative especially if you aren't trying to combine Python with any OSX specific stuff. –  Mar 14 '11 at 17:42
  • Hmm. It looks interesting but I don't feel like coughing up the cash. – Tyler Crompton Mar 14 '11 at 17:45
  • Komodo Edit is their free option. I have IDE and if you do serious Python work it is well worth it. –  Mar 14 '11 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 '11 at 10:30
  • 1
    What the hell? A question for future? Dated Mar 11 '2011 at 18:40??? – Prajeet Shrestha Jun 11 '15 at 14:57
  • @PrajeetShrestha I update it from time to time to keep it relevant. – Tyler Crompton Jun 12 '15 at 04:44
  • @TylerCrompton I don't really thing it is a good ideia to update the version of this question. I mean, the answer (which is upvoted and marked) will probably not be correct for a new version. People will come here looking at how to use it in v7, and the answer will not provide that (just an example, haven't checked if the current answer is for v7). – amfcosta Feb 17 '17 at 15:15
  • @amfcosta, I update the answer whenever I update the question. – Tyler Crompton Feb 18 '17 at 07:31
  • @TylerCrompton I still think that a new version deserves a separate question and answer. People looking to do it on an older version would not find it... – amfcosta Feb 18 '17 at 23:00
  • @TylerCrompton: Please stop updating the version numbers of this question; whilst you're updating the accepted answer, you're *not* updating the other answers (which are still for 4, 5, and 6), which leaves a broken window. Where the steps change between major versions, you should ask another question instead. – Matt Feb 20 '17 at 13:09
  • @Matt, First, all of the other answers specifically mention the version number that the answer is for. Second, I have also kept the second most upvoted question up to date. Third, it's uncommon to have a majorly out of date Xcode version installed. Notice that nobody has complained that they are using an older version that the version that the question is for. In fact, people are recommending updates. Fourth, instructions for older versions are still accessible in the revision history. There is no reason to impede this question. – Tyler Crompton Feb 20 '17 at 17:17
  • @TylerCrompton: (1) Exactly; hence the broken window (e.g. answers to "How does one create a Python friendly environment in Xcode 7" starting off with "In XCode 4..."). (2) You made one edit to update the steps when the link content changed; you haven't kept it up to date w.r.t the question (nor should you). (3, 4) It is unreasonable to expect people to check the revision history. I don't understand why you rolled back my edit; it is an improvement over the previous for the reasons outlined in (1). – Matt Feb 22 '17 at 13:39
  • Note that "how to handle updated versions" has been widely discussed on meta, and the consensus have either been to a) post a new answer, b) append to *your* answer, or c) post a new question; never to re-write an question and answer. You might find http://meta.stackoverflow.com/q/338972/444991 or http://meta.stackoverflow.com/q/265433/444991 related. If you wish to dispute this further, I would suggest opening a post on meta to discuss this, where the community can add *their* input as well. – Matt Feb 22 '17 at 13:40

8 Answers8

266

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.

Tyler Crompton
  • 11,740
  • 12
  • 59
  • 91
  • 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 '11 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 '11 at 12:08
  • @Albert, No, I have not had any problems with auto-indentation (yet). Lol. Don't jinx it. @Tom, I am not too familiar with the `pdb` module as I have not needed it yet. Print statements have sufficed for me so far. I would imagine it would, though. You can use input() (or raw_input() in Python 2) so I imagine that it work as if you are running it in Terminal or something similar. – Tyler Crompton Mar 30 '11 at 20:20
  • @Albert, you jinxed it. I think the auto-indentation still behaves as if you are using one if its native languages such as C, Objective-C, and C++. – Tyler Crompton Apr 12 '11 at 05:02
  • @Tyler Crompton Wow ! I will not use this description, as I have no Mac computer, but I upvote for the pretty work you have done and the fair info you gave for the interested persons. – eyquem Jul 27 '11 at 12:52
  • I started learning Python today and found this very helpful. Thank you! – Harold Cavendish Dec 26 '11 at 12:05
  • This seems to work only with the python that comes with mac. I tried using my /library/frameworks/..../python2.7 and when I used ⇧⌘G to link it, it wouldn't let me open that folder. Anyone else have this problem? I use pyCharm anyway but it's really cool to know that you can do this. :) – lciamp Jun 22 '12 at 22:48
  • 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. – Tyler Crompton Jun 23 '12 at 06:05
  • I got this solution to work with Xcode 4.6 and Python2.x; make sure you pay attention to Step 20 if your path has any spaces. – Greg M. Krsak Feb 21 '13 at 04:25
  • Where is the `"Base Expansions On" field`? – xxmbabanexx Mar 09 '13 at 16:33
  • 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. – Tyler Crompton Mar 28 '13 at 13:41
  • @TylerCrompton Could the build tool be my virtualenv python? How would I set that up? – Jonathan Komar Nov 18 '13 at 15:11
  • 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 '13 at 21:02
  • @macmadness86, yes, in Step 6, just use the binary stored within the virtual environment. If you don't know where this is, activate your virtual environment (if you haven't already done so) and use the `which` command as noted in Step 6. – Tyler Crompton Nov 29 '13 at 08:29
  • +1 for "which python3". When downloading Python3, it was difficult to find where the executable for it was placed, so this helped. – warship Sep 13 '14 at 05:48
  • Just adding that in version 6.1.1 there still seems to be no auto-indentation for Python. I suppose there's no hack to make this possible? – Tango Mar 10 '15 at 08:42
  • Do you have any good option on how to get provide input through xcode somehow? (I.e. That is getting either input() or raw_input() to work from within XCode?) – holroy Sep 28 '15 at 12:31
  • @holroy include `< filename` as your last argument passed on launch. – Tyler Crompton Sep 28 '15 at 16:37
  • There is no way to get it interactive using XCode? Using `< filename` is a rather static approach... – holroy Sep 28 '15 at 16:41
  • Not that I'm aware of. Sorry. – Tyler Crompton Sep 28 '15 at 17:21
  • @TylerCrompton i have various versions of python installed and all of their executables are greyed out when attempting to select them. i have searched online and found that there are many people reporting this problem but haven't come across a solution. what determines if an executable is a valid selection for xcode? – Gil Hiram Jul 30 '16 at 11:56
  • Now that you mention it, I noticed that I’m experiencing the same problem. I got around it by editing the `LaunchAction` element in the scheme file (hidden within the project file itself) by setting `selectedDebuggerIdentifier` to the empty string, setting `selectedLauncherIdentifier` to `"Xcode.IDEFoundation.Launcher.PosixSpawn"`, and adding `` as a child element. – Tyler Crompton Jul 31 '16 at 22:39
  • In Xcode 8, Step 3 is now: Select "Cross-platform" instead of "Other" – James Paul Mason Jan 31 '17 at 00:41
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.)

Community
  • 1
  • 1
Chen-Hai Teng
  • 693
  • 6
  • 16
  • 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. :/ – Tyler Crompton Dec 27 '11 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?)

Deesbek
  • 725
  • 2
  • 11
  • 27
Asher
  • 2,298
  • 5
  • 25
  • 39
  • 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... – TraxusIV Oct 01 '15 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 '16 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 '17 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.

Edvinauskas
  • 206
  • 4
  • 11
  • 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. – Tyler Crompton Mar 16 '11 at 19:14
  • Yeah, once you add the Python interpreter your good to go. If you have any questions feel free to ask. – Edvinauskas Mar 17 '11 at 01:59
  • 1
    How do you add Python interpreter to do interactive coding here? – SexyBeast Jun 06 '14 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.

BoltClock
  • 630,065
  • 150
  • 1,295
  • 1,284
Smitty
  • 69
  • 1
2

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

neowinston
  • 6,762
  • 9
  • 49
  • 80
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.

Galen
  • 581
  • 5
  • 8
  • That trick with the left-click and 'quick-look' is great! helped me get it work! in Xcode 9.2 many thanks! – novski Jan 09 '18 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.

Mohawke
  • 39
  • 1
  • 1
  • 5