Python in Xcode 4

本文详细介绍了如何在Xcode环境中配置Python项目,包括创建项目、设置构建工具、调试参数等步骤,让开发者能在Xcode中享受代码补全、自动缩进及语法高亮等功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转自: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.)

看起来你在提到“Python headers are missing”的时候信息不太完整。我假设你想说的是在使用某些库或工具时遇到缺少头文件的问题,这通常发生在需要编译C/C++扩展的情况下。 当你安装一些依赖于C语言编写的第三方包时,有时会遇到错误提示说找不到 Python 的头文件 (headers) 或者开发库 (libraries),这是因为你的系统上没有安装 Python 开发环境的相关组件,比如 `python.h` 文件等。解决这个问题的办法取决于你使用的操作系统: 对于 **Linux** 用户来说,可以尝试通过包管理器来安装 Python 的开发版本。例如,在基于 Debian/Ubuntu 系统下运行命令: ```bash sudo apt-get update && sudo apt-get install python3-dev # 对应 Python 3.x 版本 ``` 如果是 **Windows** 上,则一般是因为选择了不需要额外配置的 Python 发行版,默认不会包含这些用于构建 C 扩展的内容;你需要确保下载的是带有 "embeddable zip file" 标记或者是从微软商店获取完整的开发者套件,并将其路径添加到系统的 PATH 中去。 而在 **macOS**, 可能也需要单独安装 Xcode Command Line Tools 来获得必要的编译工具链支持。 ```bash xcode-select --install brew install python # 如果你是用 Homebrew 包管理器的话也可以考虑这样做 ``` 此外,请检查是否已经正确设置了虚拟环境中关于 INCLUDE 和 LIBRARY 路径的信息,以及确认 pip 已经是最新的版本,因为旧版本可能存在兼容性问题导致无法找到正确的资源位置。 如果你只是单纯地想了解如何处理 "headers are missing" 错误而不涉及具体的项目场景,那么以上就是通用指导建议了。当然如果还有更详细的上下文背景或者其他具体的技术栈相关联,那将有助于给出更为精准的帮助方案!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值