在Xcode7 里编写运行 Python 代码


  • 查看Python版本

    • $ python -V

    • Python 3.5.1


    • 【我这里以Python3.5.1为例,系统自带的是2.x,如果自己需要安装3.x】安装方法:参考前面几篇博客

    • http://my.oschina.net/u/868062/blog/631185

    • http://my.oschina.net/u/868062/blog/632555

    • http://my.oschina.net/u/868062/blog/631173


在Xcode下编写运行Python文件

  • Xcode 环境配置

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

      在Xcode7 里编写运行 Python 代码_第1张图片

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

      在Xcode7 里编写运行 Python 代码_第2张图片

    • 默认配置如下

      在Xcode7 里编写运行 Python 代码_第3张图片

    • 修改配置【executable】可执行文件 其他的默认即可


      在Xcode7 里编写运行 Python 代码_第4张图片

    • 配置好了,接下来创建一个python文件,抒写代码

      在项目中 New-->File-->Other-Empty 创建一个空文件 命名为main.py

      在Xcode7 里编写运行 Python 代码_第5张图片

      在Xcode7 里编写运行 Python 代码_第6张图片

    • 接下来再次Edit Schem-->Arguments把main.py添加的文件添加到Arguments Passed On Launch

      在Xcode7 里编写运行 Python 代码_第7张图片

    • 再配置一下Options-->Working Directory 选择文件坐在目录我这里(/Users/apple/Desktop/pythonTest)

      在Xcode7 里编写运行 Python 代码_第8张图片

    • 接下来抒写代码 运行

      print ('hello Python')

    • Run

      在Xcode7 里编写运行 Python 代码_第9张图片


    在终端直接执行Python文件

  • 打开终端cd到Python文件目录(也可直接把Python文件拖拽到终端)


  • 直接运行文件

    $ Python main.py

  • 报错

      File "main.py", line 2

        print 'hello Python'

                           ^

    SyntaxError: Missing parentheses in call to 'print'

  • python3取消了这种用法。使用 print('hello Python")或print('hello python')

  • 改一下代码 继续执行

    print ('hello Python')


  • $ Python main.py

    在Xcode7 里编写运行 Python 代码_第10张图片

你可能感兴趣的:(python,Xcode7)