在 Xcode 里编译运行 Python 代码

Xcode有着良好的自动补全功能和自动缩进功能,而且Xcode本身也是iOS开发唯一的选择。我们是否可以在这样一个集成环境里,搭建python运行环境呢?答案是可以的,而且很简单,运行的时候也很方便。下面来简单讲解如何在Xcode 5下,搭建python编译环境来编写和运行python程序。

 

指引 / 跳至

  • 1找到 Python

  • 2Xcode 编译 Python 程序

  • 3Edit Scheme

  • 4Build and Run

 

找到 Python

每个OS X系统都内置了python,我们可以在[实用工具][终端]里,运行which python来查看python的安装位置,方便我们调用它来搭建python的编译环境。它一般在/usr/bin/python这个位置。

which_python

Xcode 编译 Python 程序

打开Xcode,选择File > New > New Project,或按下Shift + Command + N,然后选择OS X下面的Other其它选项卡,并选择[External Build System],继续下一步

new project with extern build system

在下一步的Build Tool里,粘贴刚刚找到的路径,例如/usr/bin/python,点击Next

name new project xcode python

Edit Scheme

完成后,在左上角的项目图表上点击一下,选择[Edit Scheme]

edit scheme

在第一个选项卡Info里,把Executable选择到你找到的python程序,例如/usr/bin/python,并把下面的Debugger选择为None

edit scheme exec

在第二个选项卡Arguments里,在第一个Arguments passed on launch里,新建一个你即将新建的.py文件,例如main.py

edit scheme arguments

在第三个选项卡Option里,勾选Working Directory并选择到Xcode Project所在的文件夹,也就是你的.py文件存放的文件夹,最后确定

edit scheme options

Build and Run

在项目上右键,选择New File新建文件,并且需要选择OS X下的Others选项卡,文件类型为Empty,命名需要为你刚刚在Scheme里添加的文件名,例如main.py

xcode python new file

现在编写代码,并点击Run,就可以实现python程序在Xcode下的编译和运行了。

xcode python build and run


你可能感兴趣的:(在 Xcode 里编译运行 Python 代码)