Follow these steps:
- Open Xcode
- Create a new project and select Other from under the OS X category when the dialog appears, and then choose External Build System:
- In the next page, give your project a name “product name” and then in the “build tool”, choose the path of your Python interpreter. If you don’t know where your Python interpreter is, open Terminal and type in which python to get the path to the interpreter, like so:
- Then save your project on disk
- From the Product menu, choose Scheme and then Edit Scheme or just Option-click the little Play button on top left of Xcode. Now you should see the Edit Scheme screen which looks like this:
- Now tap on the Info tab on top of the dialog and then press on the Executable combo-box (which currently says “None”) and then from the list, choose “other…”
- An open-dialog will appear waiting for you to select your build tool, again! This is a bug in Xcode. So press the Cmd+Shift+G button in the open-dialog and when the “Go to the folder” dialog appears, enter the path of your Python interpreter again like so:
- Once you are done, press the Go button and then press the Choose button
- Back in the Edit Scheme dialog, uncheck the “Debug executable option as you don’t want Xcode to attach the LLDB debugger to Python. That’s not useful. This step is very important.
- Now tap on the Arguments tab and then under the “Arguments Passed on Launch”, press the + (plus) button and then type in “test.py” without the quotation marks, like so:
- Now tap on the Options tab and then under the “Working directory” section, tap the “Use custom working directory” and then tap on the little Folder button. Once the open-dialog appears, choose the root folder of your Xcode project:
- Now press the Close button to close the Edit Scheme dialog
- Press the Cmd+N combination on keyboard or just select from the menus, File->New->File…
- In the New file dialog, from the left hand side, choose OS X and then Other and then choose Empty and then press the Next button:
- Name your file “test.py” (without the quotation marks) and then ensure that you are saving it under your project’s main folder, the same folder that you set your “Working directory” to a few steps ago. Once you are done, press the Create button.
- Write a simple Python script in your “test.py” file like so:
- Now run your application and have a look at the console in Xcode to see your Python script successfully executed:
That was it really. Good luck everyone. If you have any questions, just let me know.
Thank you so much. It was useful tutorial. I have a question that if i import sys, it works fine, however i tried to import numpy, then it shows me error that there is no module, but in terminal everything works just fine
Any clue, and give advice how to configure it
Good post. I have a similar question as tuvu, I can “import matlibplot” but not “import pyradi” (which is a particular python package). Both work in python when launched from the terminal but not from within Xcode. I have read on some other post that the “path” used with in terminal is different from Xcode, is this where the problem lies? If so, where or what does one modify to fix this issue.
John
Hi John,
In Xcode schema settings, do this:
1. On the left hand side, select the Run action.
2. On the right hand side, select the Arguments tab.
3. At the bottom of the screen, you will see a section named “Environment Variables”. Here, you will need to set your custom paths.
I hope this helps.
Any idea why the install of matlibplot module makes the module available within Xcode for python but not the pyradi module? I do not know where to look to find out how Xcode is finding the matlibplot module. I am guessing the pip of pyradi did not set some variable used by Xcode to find the pyradi module. Thoughts? I will try your suggestion but I am not clear on where the path needs to point to. Thanks again. John
I followed all the steps but I received the following message
Launch error
There is a problem launching posix_spawn (error code: 86).
I tried the same steps as you mentioned. when I ran the test script at the last step, getting the following error message.
/usr/bin/python: can’t open file ‘test.py’: [Errno 2] No such file or directory
Cool. I was able to have it build my project with cocoa by inputting setup.py py2app in the arguments. I still have to launch the app from the finder, however. Any way to have it launch as well?
I got the same message as Rg. I looked through the directories to find test.py, and found a subdirectory that contains it. I went back into edit scheme to select the right directory, and it worked.
Thanks for the procedure VANDAD NP!
Got the error message “There is an problem launching using posix_spawn (error code: 8).” when I tried to run the code.
Wow, for once I’ve found an online tutorial which actually works without issues and is straightforward. Props to the author!