Write and run Python code in Xcode7



Write and run Python files under Xcode

  • Xcode environment configuration

    • Open Xcode , selectFile > New > New Project , or press Shift + Command + N , then select the Other tab at the bottom , and select [ External Build System ] to continue to the next step

    • When done, click once on the project chart in the upper left corner and select [ Edit Scheme ]

    • The default configuration is as follows

    • Modify the configuration [ executable ] other default executable files


    • Once configured, create a python file and write the code

      Create an empty file named main.py in New-->File-->Other-Empty in the project

    • Next, Edit Schem--> Arguments add the file added by main.py to Arguments Passed On Launch

    • Configure again Options-->Working Directory Select the file to sit in the directory I am here (/ Users/apple/Desktop/pythonTest)

    • Next, write the code to run

      print ('hello Python')

    • Run


    Execute Python files directly in the terminal

  • Open the terminal and cd to the Python file directory (you can also drag and drop the Python file directly to the terminal)

  • run the file directly

    $ Python main.py

  • report an error

      File "main.py", line 2

        print 'hello Python'

                           ^

    SyntaxError: Missing parentheses in call to 'print'

  • python3 does away with this usage. Use print('hello Python") or print('hello python')

  • Change the code to continue execution

    print ('hello Python')


  • $ Python main.py

    

{{o.name}}
{{m.name}}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324158439&siteId=291194637