Python in Xcode 4+

Python in Xcode 4+?

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.

PyObjC on Xcode 4

The Python-Cocoa class templates you're looking for are part of PyObjC, but Apple stopped including them in the install around the end of Leopard (I think), because they got out of sync with Xcode. You can download the latest versions yourself from the PyObjC repository. According to this recent guide I found, they should go in ~/Library/Developer/Xcode/Templates/.

Beware, however: according to Ronald Oussoren, PyObjC's developer, the Interface Builder part of Xcode 4 is not at all compatible with PyObjC. He made a posting to the pythonmac mailing list and another to the pyobjc-dev list suggesting that bugs be filed with Apple, requesting continued Xcode support for the bridge. There's nothing that can be done on either his or your end; Apple has to do this.

You'll have to keep Xcode 3 around to do PyObjC GUI stuff.

How to run Python script before each build in Xcode?

I managed to get this working by using /bin/sh as my shell and python test.py as my script.

Using xcode 4 to read c source

You can find a similar list of symbols in the current file by clicking the popup menu above the editor in Xcode (or using the keyboard shortcut Ctrl+6). You can also filter this list by starting to type after the menu shows up.

screenshot

Xcode doesn't have a dedicated panel for this like PyCharm. There is an inspector panel for viewing all symbols in the current project/workspace (Cmd+2), but it can't be filtered to show only symbols in the current file.

The Python command requires the command line developer tools loop with Xcode 13.4.1 on macOS 12.5

I finally found a fix.

Steps I did to make it work:

  1. Removed all symlinks I've set originally
  2. Removed python installed via pyenv using pyenv uninstall 3.10.6
  3. Ran brew uninstall pyenv to removepyenv
  4. brew install python
  5. python --version results in Python 3.9.13
  6. And finally sudo ln -s $(which python3) /usr/local/bin/python

So essentially if you're on a new system do steps 4 and 6 and you should be good to go.



Related Topics



Leave a reply



Submit