Building and Running Python Scripts with Xcode 6.1

Follow these steps:

  1. Open Xcode
  2. Create a new project and select Other from under the OS X category when the dialog appears, and then choose External Build System:

    Screen Shot 2014-10-20 at 20.20.47

    Tap to enlarge

  3. 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:

    Tap to enlarge

    Tap to enlarge

  4. Then save your project on disk
  5. 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:

    Tap to enlarge

    Tap to enlarge

  6. 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…

    Tap to enlarge

    Tap to enlarge

  7. 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:

    Tap to enlarge

    Tap to enlarge

  8. Once you are done, press the Go button and then press the Choose button
  9. 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.

    Tap to enlarge

    Tap to enlarge

  10. 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:

    Tap to enlarge

    Tap to enlarge

  11. 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:

    Tap to enlarge

    Tap to enlarge

  12. Now press the Close button to close the Edit Scheme dialog
  13. Press the Cmd+N combination on keyboard or just select from the menus, File->New->File…
  14. 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:

    Tap to enlarge

    Tap to enlarge

  15. 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.

    Tap to enlarge

    Tap to enlarge

  16. Write a simple Python script in your “test.py” file like so:

    Tap to enlarge

    Tap to enlarge

  17. Now run your application and have a look at the console in Xcode to see your Python script successfully executed:

    Tap to enlarge

    Tap to enlarge

That was it really. Good luck everyone. If you have any questions, just let me know.

Advertisements

11 thoughts on “Building and Running Python Scripts with Xcode 6.1

  1. 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

  2. Pingback: Build and Run Python Scripts Using Xcode 5 | Vandad NP
  3. 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

  4. 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

  5. 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?

  6. 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!

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