Building and Running Python Scripts with Xcode 4.x

 Edit 1 (20th October 2014): Xcode 6.1 instructions can be found here: https://vandadnp.wordpress.com/2014/10/20/building-and-running-python-scripts-with-xcode-6-1/

So you have installed Python and you want to use Xcode to write and run your Python scripts… is that correct? Ok then, follow these steps to create a simple project and run it with Python using Xcode 4.x…

  • Open Xcode and select File->New->Project…
  • From the left hand side, under the OS X category, select Other
  • Now on the right hand side, select External Build System and press the Next button, as shown below

Click to Enlarge

  • Now open a Terminal window and type which python in it, and keep the results in mind or just keep Terminal open for future reference, as shown below. This will tell you where Python is installed on your system.

Click to Enlarge

  • As you can see, in my case Python is installed under /usr/bin/Python
  • Now back to Xcode, set the following values on the second screen of project creation:
    • Product Name: test
    • Organization Name: company
    • Company Identifier: com.company
    • Build Tool: enter the path to Python which we retrieved from Terminal. I have entered /usr/bin/Python (see below)
  • Once you are done entering all four values, press the Next button.

Click to Enlarge

  • Once you pressed Next, now you should choose where you want to save your project, save it somewhere that makes sense for you. I am going to save it in ~/Desktop/Development/Projects/Python/Sandbox/test/ as that’s how I have structured my development environment. Once you are happy with the folder that you are saving your project in, press the Create button in Xcode’s dialog, as shown below

Click to Enlarge

  • Now select the “test” target from the breadcrumb menu on top left corner of Xcode’s main window and choose Edit Scheme, as shown below

Click to Enlarge

  • In the Edit Scheme screen, on the left hand side, choose the Run option. Then on the right hand side, choose the Executable section and press the Other… item. This will open an Open dialog for you asking you to choose the binary of Python. For me, Python was sitting in /user/bin/python as you saw earlier. So in Mac, I press Cmd+Shift+G to “Go to Folder” and then I type in /usr/bin/, as shown below and then press Go:

Click to Enlarge

  • Now that you are in the folder that contains the Python binary, find and pick the binary and press the Choose button
  • Back in Xcode, on the same Scheme window that we were on before, under the Debugger section under Info, make sure that you choose None. This is very important.

Click to Enlarge

  • Back in Xcode, on the same Scheme window that we were on before, navigate on the right hand side to the Arguments tab and under the Arguments Passed On Launch, press the + button and type test.py, as shown below:

Click to Enlarge

  • Now go to the Options tab, under the Working Directory, click the Use custom working directory and then put the full path to where you are intending to save your Python files in this project. My project is at ~/Desktop/Development/Projects/Python/Sandbox/test/ and I’m intending to save my first file as ~/Desktop/Development/Projects/Python/Sandbox/test/test.py so I will choose ~/Desktop/Development/Projects/Python/Sandbox/test/ as my working directory, as shown below:

Click to Enlarge

  • We are now done with the Schemes. Press the OK button to close this window.\
  • In Xcode, from the Menus, choose File->New->File…
  • From the left hand side of the dialog, under the OS X section, choose Other and then on the right hand side, choose Empty and press the Next button, as shown below:

Click to Enlarge

  • In the next screen, make sure that you set the value of Save As as test.py. This is what we have been preparing our Scheme for, that we are going to have a file called test.py that needs to be passed to the Python interpreter. Have a look at the following screenshot. Once you are done, press the Create button:

Click to Enlarge

  • Now enter the following code in your test.py file: print “It works!”, as shown below:

Click to Enlarge

  • As the final step to make sure everything is working fine, in Xcode, from the menus, choose Product->Run, the Debugging Area will popup and show you the output of the print command like so:

Click to Enlarge

Lovely, it’s all working. I hope this tutorial helped you. If you have any questions or suggestions, please leave them in the comment sections down below…

Advertisements

41 thoughts on “Building and Running Python Scripts with Xcode 4.x

  1. In the bit where you are choosing the executable, I am trying to put in usr/bin/python but the file doesn’t exist seemingly. I have found a python binary in a different location…will that be ok? Also once I’ve done that I don’t have an arguments tab…

  2. Thank you fot the tutorial!
    I have a problem with Xcode 4.3. It seems that changing “Working Directory” has no effect and python is searching for “test.py” in the root directory (“/”). My current workaround is to add “full/path/to/test.py” into Arguments. Any ideas why it went wrong?

    To be more specific, I get this error in the Debugging Area: “/usr/bin/python: can’t open file ‘test.py’: [Errno 2] No such file or directory”.

  3. Thanks, this worked using EPD python and /Library/Frameworks/EPD64.framework/Versions/Current/bin/python
    Now to see if some other things work.

  4. So here is my next question – Just why would I want to use Xcode (instead of Eclipse, for instance)? I guess that it has some packaging benefits: on one project I did in eclipse I ran into a problem with @rpath. If I use Xcode will these kinds of issues be resolved? Anyone have experience?

    • Hi Paul. For me, I literally do almost all my development in Xcode, so it’s an IDE that I know my way around. It’s best to use it, at least for me, than to have to learn a new IDE like Eclipse.

  5. In my xcode the number line does not appear.
    Neither does the indent automatically.
    And when you fail, it does not colored your fail. Only it says the number line which doesn’t appear as I tell you before.

    My version of xcode is 4.5.2. Is because of that? How can get this utilities?

    Thanks a lot.

  6. I truly appreciate your tutorial. When I get all the way through and type “it works”, I get the message “Xcode cannot run using the selected destination. Choose a destination with a supported architecture in order to run on this system..”

    Any ideas?

  7. I have followed your steps and everything seems to be OK until I run it. When I run it, I get the following error in the All Output area in Xcode:
    /Library/Frameworks/Python.framework/Versions/3.3/Resources/Python.app/Contents/MacOS/Python: can’t open file ‘test.py’: [Errno 2] No such file or directory

    I am trying to use python 3.3. Other than that, I think it’s a normal system.

  8. I finally found my problem. I was trying to use python 3.3. This process works for python 2,.7 but not 3.3. I’m happy with using 2.7 and will pursue 3.3 another day.

    Thanks for the help.

  9. Thank you for these instructions!
    but will I have to repeat all of these steps just to start a new project in Python?

  10. When I run a simple test like input a number, I can not input a number. I know I’m missing something but I do not know what it is.

  11. Pingback: logbook « Python in XCode

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s