1 .. _tut-using: 2 3 **************************** 4 Using the Python Interpreter 5 **************************** 6 7 8 .. _tut-invoking: 9 10 Invoking the Interpreter 11 ======================== 12 13 The Python interpreter is usually installed as :file:`/usr/local/bin/python3.6` 14 on those machines where it is available; putting :file:`/usr/local/bin` in your 15 Unix shell's search path makes it possible to start it by typing the command: 16 17 .. code-block:: text 18 19 python3.6 20 21 to the shell. [#]_ Since the choice of the directory where the interpreter lives 22 is an installation option, other places are possible; check with your local 23 Python guru or system administrator. (E.g., :file:`/usr/local/python` is a 24 popular alternative location.) 25 26 On Windows machines, the Python installation is usually placed in 27 :file:`C:\\Python36`, though you can change this when you're running the 28 installer. To add this directory to your path, you can type the following 29 command into the command prompt in a DOS box:: 30 31 set path=%path%;C:\python36 32 33 Typing an end-of-file character (:kbd:`Control-D` on Unix, :kbd:`Control-Z` on 34 Windows) at the primary prompt causes the interpreter to exit with a zero exit 35 status. If that doesn't work, you can exit the interpreter by typing the 36 following command: ``quit()``. 37 38 The interpreter's line-editing features include interactive editing, history 39 substitution and code completion on systems that support readline. Perhaps the 40 quickest check to see whether command line editing is supported is typing 41 :kbd:`Control-P` to the first Python prompt you get. If it beeps, you have command 42 line editing; see Appendix :ref:`tut-interacting` for an introduction to the 43 keys. If nothing appears to happen, or if ``^P`` is echoed, command line 44 editing isn't available; you'll only be able to use backspace to remove 45 characters from the current line. 46 47 The interpreter operates somewhat like the Unix shell: when called with standard 48 input connected to a tty device, it reads and executes commands interactively; 49 when called with a file name argument or with a file as standard input, it reads 50 and executes a *script* from that file. 51 52 A second way of starting the interpreter is ``python -c command [arg] ...``, 53 which executes the statement(s) in *command*, analogous to the shell's 54 :option:`-c` option. Since Python statements often contain spaces or other 55 characters that are special to the shell, it is usually advised to quote 56 *command* in its entirety with single quotes. 57 58 Some Python modules are also useful as scripts. These can be invoked using 59 ``python -m module [arg] ...``, which executes the source file for *module* as 60 if you had spelled out its full name on the command line. 61 62 When a script file is used, it is sometimes useful to be able to run the script 63 and enter interactive mode afterwards. This can be done by passing :option:`-i` 64 before the script. 65 66 All command line options are described in :ref:`using-on-general`. 67 68 69 .. _tut-argpassing: 70 71 Argument Passing 72 ---------------- 73 74 When known to the interpreter, the script name and additional arguments 75 thereafter are turned into a list of strings and assigned to the ``argv`` 76 variable in the ``sys`` module. You can access this list by executing ``import 77 sys``. The length of the list is at least one; when no script and no arguments 78 are given, ``sys.argv[0]`` is an empty string. When the script name is given as 79 ``'-'`` (meaning standard input), ``sys.argv[0]`` is set to ``'-'``. When 80 :option:`-c` *command* is used, ``sys.argv[0]`` is set to ``'-c'``. When 81 :option:`-m` *module* is used, ``sys.argv[0]`` is set to the full name of the 82 located module. Options found after :option:`-c` *command* or :option:`-m` 83 *module* are not consumed by the Python interpreter's option processing but 84 left in ``sys.argv`` for the command or module to handle. 85 86 87 .. _tut-interactive: 88 89 Interactive Mode 90 ---------------- 91 92 When commands are read from a tty, the interpreter is said to be in *interactive 93 mode*. In this mode it prompts for the next command with the *primary prompt*, 94 usually three greater-than signs (``>>>``); for continuation lines it prompts 95 with the *secondary prompt*, by default three dots (``...``). The interpreter 96 prints a welcome message stating its version number and a copyright notice 97 before printing the first prompt: 98 99 .. code-block:: shell-session 100 101 $ python3.6 102 Python 3.6 (default, Sep 16 2015, 09:25:04) 103 [GCC 4.8.2] on linux 104 Type "help", "copyright", "credits" or "license" for more information. 105 >>> 106 107 .. XXX update for new releases 108 109 Continuation lines are needed when entering a multi-line construct. As an 110 example, take a look at this :keyword:`if` statement:: 111 112 >>> the_world_is_flat = True 113 >>> if the_world_is_flat: 114 ... print("Be careful not to fall off!") 115 ... 116 Be careful not to fall off! 117 118 119 For more on interactive mode, see :ref:`tut-interac`. 120 121 122 .. _tut-interp: 123 124 The Interpreter and Its Environment 125 =================================== 126 127 128 .. _tut-source-encoding: 129 130 Source Code Encoding 131 -------------------- 132 133 By default, Python source files are treated as encoded in UTF-8. In that 134 encoding, characters of most languages in the world can be used simultaneously 135 in string literals, identifiers and comments --- although the standard library 136 only uses ASCII characters for identifiers, a convention that any portable code 137 should follow. To display all these characters properly, your editor must 138 recognize that the file is UTF-8, and it must use a font that supports all the 139 characters in the file. 140 141 To declare an encoding other than the default one, a special comment line 142 should be added as the *first* line of the file. The syntax is as follows:: 143 144 # -*- coding: encoding -*- 145 146 where *encoding* is one of the valid :mod:`codecs` supported by Python. 147 148 For example, to declare that Windows-1252 encoding is to be used, the first 149 line of your source code file should be:: 150 151 # -*- coding: cp-1252 -*- 152 153 One exception to the *first line* rule is when the source code starts with a 154 :ref:`UNIX "shebang" line <tut-scripts>`. In this case, the encoding 155 declaration should be added as the second line of the file. For example:: 156 157 #!/usr/bin/env python3 158 # -*- coding: cp-1252 -*- 159 160 .. rubric:: Footnotes 161 162 .. [#] On Unix, the Python 3.x interpreter is by default not installed with the 163 executable named ``python``, so that it does not conflict with a 164 simultaneously installed Python 2.x executable. 165