1.找到Python位置:python
終端輸入:which pythonui
獲取python的安裝位置,通常爲/usr/bin/python。獲取到這個路徑方便使用它來搭建python的編譯環境。code
2.在Xcode中建立python程序orm
打開Xcode,新建工程(Shift+Command+N),選擇Cross-platform->Ohter->External Build System,繼續下一步blog
在Build Tool中粘貼剛剛找到的python路徑,eg:/usr/bin/python,點擊Next:it
3.設置Edit Schemeio
建立好項目以後,在左上角的項目圖表上點擊一下,選擇Edit Scheme編譯
第一個選項Info中的Executable選擇python的路徑,eg:/usr/bin/pythontable
第二個選項Arguments裏,在第一個Arguments passed on launch裏,新建一個你即將新建的.py文件,eg:main.pyform
第三個選項Option裏,勾選Working Directory並選擇到Xcode Project所在的文件夾,也就是你的.py文件存放的文件夾,最後選擇肯定。
3.Build and Run
在項目中新建文件(Command+N),選擇macOS->Other->Empty,命名爲剛剛在Scheme裏添加的文件名,eg:mian.py
如今編寫代碼,並點擊Run, 就能夠實現python程序在Xcode下的編譯和運行了。