Xcode 4+中的Python?

问题:Xcode 4+中的Python?

如何在Xcode 4、5、6或7中创建Python友好环境?

How does one create a Python friendly environment in Xcode 4, 5, 6 or 7?


回答 0

我想到了!这些步骤使它看起来比实际需要花费更多的精力。

这些说明用于从头开始创建项目。如果您希望将现有的Python脚本包含在此项目中,则显然需要稍微偏离这些说明。

如果您发现这些说明不再起作用或由于Xcode更新的更改而不清楚,请通知我。我将进行必要的更正。

  1. 打开Xcode。两者的说明相同。
  2. 在菜单栏中,单击“文件”→“新建”→“新建项目…”。
  3. 在左窗格中选择“其他”,然后在右页面中选择“外部构建系统”,然后单击“下一步”。
  4. 输入产品名称,组织名称或组织标识符。
  5. 在“构建工具”字段中,输入/usr/local/bin/python3Python 3或/usr/bin/pythonPython 2,然后单击“下一步”。请注意,这假设您具有解析为Python可执行文件的符号链接(默认情况下已设置)。如果不确定Python可执行文件的位置,请在Terminal:which python3和中输入以下命令之一which python
  6. 点击下一步”。
  7. 选择保存位置,然后单击“创建”。
  8. 在菜单栏中,单击“文件”→“新建”→“新建文件…”。
  9. 在“ OS X”下选择“其他”。
  10. 选择“空”,然后单击“下一步”。
  11. 导航到项目文件夹(否则它将不起作用),输入Python文件的名称(包括“ .py”扩展名),然后单击“创建”。
  12. 在菜单栏中,单击“产品”→“方案”→“编辑方案…”。
  13. 单击左窗格中的“运行”。
  14. 在“信息”选项卡中,单击“可执行文件”字段,然后单击“其他…”。
  15. 从步骤5导航到可执行文件。您可能需要使用 ⇧⌘G。如果目录是隐藏的,则来键入目录。
  16. 选择可执行文件,然后单击“选择”。
  17. 取消选中“调试可执行文件”。如果跳过此步骤,Xcode将尝试调试Python可执行文件本身。我不知道将外部调试工具集成到Xcode中的方法。
  18. 单击“启动时传递的参数”下的“ +”图标。您可能需要通过单击指向右侧的三角形来扩展该部分。
  19. 输入$(SRCROOT)/(或$(SOURCE_ROOT)/),然后输入您要测试的Python文件的名称。请记住,Python程序必须位于项目文件夹中。否则,您将必须在此处输入完整路径(或相对路径(如果位于项目文件夹的子文件夹中))。如果完整路径中的任何地方都有空格,则必须在此开头和结尾处都使用引号。
  20. 点击“关闭”。

请注意,如果在“显示文件检查器”选项卡处于活动状态的情况下打开“实用程序”面板,则文件类型将自动设置为“默认-Python脚本”。随意查看它具有的所有文件类型选项,以了解其所有功能。上面的方法可以应用于任何解释语言。到目前为止,我还没有弄清楚如何使它与Java一起工作。再说一次,我还没有做太多的研究。当然,网上有一些关于这一切的文件。

在没有管理特权的情况下运行:

如果您没有管理特权或不在Developer组中,则仍可以使用Xcode进行Python编程(但仍然无法使用需要编译的语言进行开发)。在菜单栏中单击“产品”→“执行操作”→“无需构建即可运行”,或仅使用键盘快捷键来代替播放按钮^⌘R

其他说明:

要更改文本编码,行尾和/或缩进设置,请打开“实用程序”面板,然后单击“显示文件检查器”选项卡。在这里,您将找到这些设置。

有关Xcode的构建设置的更多信息,没有比更好的资源了。我希望听到有人将其与不受支持的编译语言一起使用的信息。此过程应适用于任何其他解释语言。只需确保相应地更改了步骤5和步骤16。

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.


回答 1

我创建了Xcode 4模板来简化Tyler提供的步骤。
结果是Xcode 4的Python项目模板。

现在,您需要做的就是下载模板,将其移动到/Developer/Library/Xcode/Templates/Project Templates/Mac/Others/,然后使用Xcode 4新建一个Python项目。

它仍然需要手动配置方案(您可以参考Tyler提供的步骤12–20 。)

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–20 provided by Tyler.)


回答 2

使Python在XCode 7中工作的过程

步骤1:使用外部构建系统设置项目

步骤1.1:编辑项目计划

步骤2:将 Python指定为项目的可执行文件(shift-command-g),路径应为/ usr / bin / python

步骤3:指定您的自定义工作目录

步骤4:将命令行参数指定为python文件的名称。(在此示例中为“ test.py”)

步骤5:谢天谢地,就是这样!

(在OSX支持python调试器之前无法添加调试功能?)

Procedure to get Python Working in XCode 7

Step 1: Setup your Project with a External Build System

Step 1.1: Edit the Project Scheme

Step 2: Specify Python as the executable for the project (shift-command-g) the path should be /usr/bin/python

Step 3: Specify your custom working directory

Step 4: Specify your command line arguments to be the name of your python file. (in this example “test.py”)

Step 5: Thankfully thats it!

(debugging can’t be added until OSX supports a python debugger?)


回答 3

您应该尝试使用Eclipse的PyDev插件。我尝试了很多与python一起使用的编辑器/ IDE,但是我最喜欢的唯一一个是Eclipse的PyDev插件。它具有代码完成,调试器和许多其他不错的功能。加上两者都是免费的。

You should try PyDev plug in for Eclipse. I tried alot of editors/IDE’s to use with python, but the only one i liked the most is the PyDev plugin for Eclipse. It has code completion, debugger and many other nice features. Plus both are free.


回答 4

我在XCode3中使用了一段时间的另一种方法是:

请参阅上面的步骤1-15。

  1. 选择/ bin / bash作为可执行文件
  2. 对于“调试器”字段,选择“无”。
  3. 在“参数”选项卡中,单击“基础扩展为”字段,然后选择您之前创建的目标。
  4. 单击“启动时传递的参数”下的“ +”图标。您可能需要通过单击指向右侧的三角形来扩展该部分。
  5. 输入“ -l”。这将告诉bash使用您的登录环境(PYTHONPATH等)。
  6. 再次执行步骤19。
  7. 输入“ -c’$(SOURCE_ROOT)/。py’”
  8. 点击“确定”。
  9. 开始编码。

这种方式的好处是,它将使用与在XCode外部运行(使用bash .profile进行设置)相同的环境进行开发。

它也足够通用,可以让您开发/运行任何类型的文件,而不仅仅是python。

Another way, which I’ve been using for awhile in XCode3:

See steps 1-15 above.

  1. Choose /bin/bash as your executable
  2. For the “Debugger” field, select “None”.
  3. In the “Arguments” tab, click the “Base Expansions On” field and select the target you created earlier.
  4. Click the “+” icon under “Arguments Passed On Launch”. You may have to expand that section by clicking on the triangle pointing to the right.
  5. Type in “-l”. This will tell bash to use your login environment (PYTHONPATH, etc..)
  6. Do step #19 again.
  7. Type in “-c ‘$(SOURCE_ROOT)/.py'”
  8. Click “OK”.
  9. Start coding.

The nice thing about this way is it will use the same environment to develop in that you would use to run in outside of XCode (as setup from your bash .profile).

It’s also generic enough to let you develop/run any type of file, not just python.


回答 5

Apple Developer Library的技术说明TN2328对我使用Xcode 5.0嵌入Python 所做的更改提供了很多帮助。

This Technical Note TN2328 from Apple Developer Library helped me a lot about Changes To Embedding Python Using Xcode 5.0.


回答 6

该线程很旧,但为了配合Xcode版本8.3.3,Tyler Crompton在接受的答案中的方法仍然有效(某些名称稍有不同,但无足轻重)。

我稍微挣扎的2点:

第16步:如果所需的python可执行文件显示为灰色,请右键单击它,然后选择快速查找。然后关闭快速查看窗口,现在它应该是可选的。

步骤19:如果这不适合您,则可以在“参数”选项卡中仅输入python文件的名称,然后在“工作目录”下的“选项”选项卡中明确输入项目根目录-选中“使用自定义工作方式目录”框,然后在其下方的字段中输入您的项目根目录。

This thread is old, but to chime in for Xcode Version 8.3.3, Tyler Crompton’s method in the accepted answer still works (some of the names are very slightly different, but not enough to matter).

2 points where I struggled slightly:

Step 16: If the python executable you want is greyed out, right click it and select quick look. Then close the quick look window, and it should now be selectable.

Step 19: If this isn’t working for you, you can enter the name of just the python file in the Arguments tab, and then enter the project root directory explicitly in the Options tab under Working Directory–check the “Use custom working directory” box, and type in your project root directory in the field below it.


回答 7

试用Editra它是免费的,具有许多很酷的功能和插件,可以在大多数平台上运行,并且是用Python编写的。我在家中以及在工作于Windows / Linux的所有非XCode开发中都使用它。

Try Editra It’s free, has a lot of cool features and plug-ins, it runs on most platforms, and it is written in Python. I use it for all my non-XCode development at home and on Windows/Linux at work.