Building and Running Python Scripts with Xcode 4.x
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
- 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.
- 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.
- 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
- 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
- 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:
- 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.
- 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:
- 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:
- 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:
- 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:
- Now enter the following code in your test.py file: print “It works!”, as shown below:
- 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:
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…












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…
If you open a Terminal and type:
“which python”
*Without* the quotation marks, what does it return?
It returns usr/bin/python. The problem is that I can find usr/bin but within that file I can’t find the python binary.
What happens if you run this command?
ls -la /usr/bin/python
I typed that in to the terminal and got this: -rwxr-xr-x 2 root wheel 86000 10 Feb 2011 /usr/bin/python…
Well great, in that case, it is confirmed that the python binary is in your /usr/bin/ folder.
Hmm thanks for the reply
Is there any way that it could be hidden at all? I have looked in the folder many times but it’s not there to view..
You know what, I typed /usr/bin rather than usr/bin and found it. It’s really weird that the / on the front makes all the difference. Which Python should I use: http://i1137.photobucket.com/albums/n506/dirac1/Screenshot2012-08-06at100105.png
just /usr/bin/python
That will do
Awesome, thanks for all of the info so far
In the scheme window, the part where you say about navigating to the Options tab and select “use custom directory” doesn’t exist on my one: http://i1137.photobucket.com/albums/n506/dirac1/Screenshot2012-08-06at101511.png
I tried to run a simple program anyway but the error is “can’t exec /usr/bin/python.
That’s bizarre. Can you please share your Xcode project with me so that I can try to open it here to find out what the issue is?
File “test.py”, line 1
SyntaxError: Non-ASCII character ‘\xe2′ in file test.py on line 1, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details
???
Sorry! my mistake:)
I am typing: print “it works!”
As you do in the tutorial…
Is there any way to have a scheme that works with any python script, so I don’t have to create a new scheme every time?
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”.
Thanks, this worked using EPD python and /Library/Frameworks/EPD64.framework/Versions/Current/bin/python
Now to see if some other things work.
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.
why xCode write this message? (In executable I choose python3.3)
” File “python1.py”, line 1
print “it works!”
^
SyntaxError: invalid syntax”
Because in python3.3 print must take brackets
you must write “print (“it works”)
unlike python 3 where you can write print “it works” (like OP)
this is because print is now a function
unlike python 2**
my bad
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.
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?
/usr/bin/python: can’t open file ‘test.py’: [Errno 2] No such file or directory
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.
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.
n’rr mind.
So how many of these instructions need to be repeated if I want to start a new program?
sweet jesus moderate my terrible inputs.
nice work dude
Thanks
I hope you enjoyed it.
Thank you for these instructions!
but will I have to repeat all of these steps just to start a new project in Python?
I believe you can simply create a Workspace in Xcode and put your .py files there and do this configuration only once for the whole workspace.
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.
Hi, how do I add another python file to the project ? File > New > test2.py runs but prints out test.py’s code.