Python in Xcode 4

来源:互联网 发布:钉钉办公软件下载 编辑:程序博客网 时间:2024/06/07 21:09

转自:http://stackoverflow.com/questions/5276967/python-in-xcode-4


  1. Open Xcode 4.
  2. In the menu bar, click "File" → "New" → "New Project…".
  3. Select "Other" under "Mac OS X".
  4. Select "External Build System" and click "Next".
  5. Enter the product name.
  6. 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 Python installed in the typical location(s). if you are unsure as to where your Python executables are enter these commands into Terminal: which python3 and which python.
  7. Choose where to save it and click "Create".
  8. In the menu bar, click "File" → "New" → "New File…".
  9. Select "Other" under "Mac 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 (include the ".py" extension), and click "Save".
  12. In the menu bar, click "Product" → "Edit Scheme…".
  13. Click "Run" in the left column.
  14. In the "Info" tab, click the "Executable" field and then click "Other…".
  15. Navigate to the executable from Step 6. You may need to use ⇧⌘G to type in the directory if it is hidden.
  16. Select the executable and click "Choose".
  17. For the "Debugger" field, select "None".
  18. In the "Arguments" tab, click the "Base Expansions On" field and select the target that is named the same as your project.
  19. Click the "+" icon under "Arguments Passed On Launch". You may have to expand that section by clicking on the triangle pointing to the right.
  20. Type in $(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.
  21. Click "OK".
  22. Start coding.

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.

Say, "Hello, code completion, auto-indentation, and syntax highlighting." Note that it's not as advanced as it is with C, C++, or Objective-C but it is better than using IDLE in my opinion.

Autoindentation:

The odd indentation is due to how Xcode auto-indents switch statements (which, of course, Python does not have).

Switch statements in a C-style language look like this:

switch(example) {case 0:    print("False");case 1:    print("True");}

Notice that I did not indent the case statements. This is how Xcode formats it for you. Once you enter the colon at the end of the case conditional statement, Xcode sometimes unindents the line (assuming it is all one line) by one tab. I have yet to figure what exactly Xcode uses to determine when to unindent a line with a colon. It seems pretty sporadic.

Notice any similarities? Yeah, C uses colons for each case which looks similar to Python syntax, so Xcode thinks you are doing some kind of switch statement for some reason. Also note that (if you have your settings configured for this) Xcode will automatically insert a closing brace (}) because it was not developed for Python development.

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.



I've created Xcode 4 templates to simplify the steps provided by Tyler.
The result is Python Project Template for Xcode 4.

Now what you need to do is download the templates, move it to/Developer/Library/Xcode/Templates/Project Templates/Mac/Others/ and then new a Python project with Xcode 4.

It still needs manual Scheme setup (you can refer to steps 12 ~ 21 provided by Tyler.)


原创粉丝点击