| Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 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/python` on | 
 | 14 | 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 |    python | 
 | 18 |  | 
 | 19 | to the shell.  Since the choice of the directory where the interpreter lives is | 
 | 20 | an installation option, other places are possible; check with your local Python | 
 | 21 | guru or system administrator.  (E.g., :file:`/usr/local/python` is a popular | 
 | 22 | alternative location.) | 
 | 23 |  | 
 | 24 | On Windows machines, the Python installation is usually placed in | 
| Georg Brandl | 9352f1c | 2010-04-10 11:16:59 +0000 | [diff] [blame] | 25 | :file:`C:\\Python27`, though you can change this when you're running the | 
| Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 26 | installer.  To add this directory to your path,  you can type the following | 
 | 27 | command into the command prompt in a DOS box:: | 
 | 28 |  | 
| Georg Brandl | 9352f1c | 2010-04-10 11:16:59 +0000 | [diff] [blame] | 29 |    set path=%path%;C:\python27 | 
| Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 30 |  | 
 | 31 | Typing an end-of-file character (:kbd:`Control-D` on Unix, :kbd:`Control-Z` on | 
 | 32 | Windows) at the primary prompt causes the interpreter to exit with a zero exit | 
 | 33 | status.  If that doesn't work, you can exit the interpreter by typing the | 
| Georg Brandl | 4d94d31 | 2009-09-18 07:22:41 +0000 | [diff] [blame] | 34 | following command: ``quit()``. | 
| Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 35 |  | 
 | 36 | The interpreter's line-editing features usually aren't very sophisticated.  On | 
 | 37 | Unix, whoever installed the interpreter may have enabled support for the GNU | 
 | 38 | readline library, which adds more elaborate interactive editing and history | 
 | 39 | features. Perhaps the quickest check to see whether command line editing is | 
| Serhiy Storchaka | 9b2e37f | 2015-09-12 17:47:12 +0300 | [diff] [blame] | 40 | supported is typing :kbd:`Control-P` to the first Python prompt you get.  If it beeps, | 
| Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 41 | you have command line editing; see Appendix :ref:`tut-interacting` for an | 
 | 42 | introduction to the keys.  If nothing appears to happen, or if ``^P`` is echoed, | 
 | 43 | command line editing isn't available; you'll only be able to use backspace to | 
 | 44 | remove characters from the current line. | 
 | 45 |  | 
 | 46 | The interpreter operates somewhat like the Unix shell: when called with standard | 
 | 47 | input connected to a tty device, it reads and executes commands interactively; | 
 | 48 | when called with a file name argument or with a file as standard input, it reads | 
 | 49 | and executes a *script* from that file. | 
 | 50 |  | 
 | 51 | A second way of starting the interpreter is ``python -c command [arg] ...``, | 
 | 52 | which executes the statement(s) in *command*, analogous to the shell's | 
 | 53 | :option:`-c` option.  Since Python statements often contain spaces or other | 
| Georg Brandl | c5a235b | 2008-05-30 19:17:29 +0000 | [diff] [blame] | 54 | characters that are special to the shell, it is usually advised to quote | 
 | 55 | *command* in its entirety with single quotes. | 
| Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 56 |  | 
 | 57 | Some Python modules are also useful as scripts.  These can be invoked using | 
 | 58 | ``python -m module [arg] ...``, which executes the source file for *module* as | 
 | 59 | if you had spelled out its full name on the command line. | 
 | 60 |  | 
| Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 61 | When a script file is used, it is sometimes useful to be able to run the script | 
 | 62 | and enter interactive mode afterwards.  This can be done by passing :option:`-i` | 
| Sandro Tosi | c93e413 | 2011-10-31 17:15:03 +0100 | [diff] [blame] | 63 | before the script. | 
| Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 64 |  | 
| Berker Peksag | 3e1c823 | 2014-12-10 02:07:08 +0200 | [diff] [blame] | 65 | All command-line options are described in :ref:`using-on-general`. | 
 | 66 |  | 
| Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 67 |  | 
 | 68 | .. _tut-argpassing: | 
 | 69 |  | 
 | 70 | Argument Passing | 
 | 71 | ---------------- | 
 | 72 |  | 
 | 73 | When known to the interpreter, the script name and additional arguments | 
| R. David Murray | 561b96f | 2011-02-11 17:25:54 +0000 | [diff] [blame] | 74 | thereafter are turned into a list of strings and assigned to the ``argv`` | 
 | 75 | variable in the ``sys`` module.  You can access this list by executing ``import | 
 | 76 | sys``.  The length of the list is at least one; when no script and no arguments | 
| Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 77 | are given, ``sys.argv[0]`` is an empty string.  When the script name is given as | 
 | 78 | ``'-'`` (meaning  standard input), ``sys.argv[0]`` is set to ``'-'``.  When | 
 | 79 | :option:`-c` *command* is used, ``sys.argv[0]`` is set to ``'-c'``.  When | 
 | 80 | :option:`-m` *module* is used, ``sys.argv[0]``  is set to the full name of the | 
 | 81 | located module.  Options found after  :option:`-c` *command* or :option:`-m` | 
 | 82 | *module* are not consumed  by the Python interpreter's option processing but | 
 | 83 | left in ``sys.argv`` for  the command or module to handle. | 
 | 84 |  | 
 | 85 |  | 
 | 86 | .. _tut-interactive: | 
 | 87 |  | 
 | 88 | Interactive Mode | 
 | 89 | ---------------- | 
 | 90 |  | 
 | 91 | When commands are read from a tty, the interpreter is said to be in *interactive | 
 | 92 | mode*.  In this mode it prompts for the next command with the *primary prompt*, | 
 | 93 | usually three greater-than signs (``>>>``); for continuation lines it prompts | 
 | 94 | with the *secondary prompt*, by default three dots (``...``). The interpreter | 
 | 95 | prints a welcome message stating its version number and a copyright notice | 
| Martin Panter | 8f1dd22 | 2016-07-26 11:18:21 +0200 | [diff] [blame^] | 96 | before printing the first prompt: | 
 | 97 |  | 
 | 98 | .. code-block:: shell-session | 
| Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 99 |  | 
 | 100 |    python | 
| Georg Brandl | 9352f1c | 2010-04-10 11:16:59 +0000 | [diff] [blame] | 101 |    Python 2.7 (#1, Feb 28 2010, 00:02:06) | 
| Neal Norwitz | 76e4d62 | 2007-11-19 01:46:20 +0000 | [diff] [blame] | 102 |    Type "help", "copyright", "credits" or "license" for more information. | 
| Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 103 |    >>> | 
 | 104 |  | 
 | 105 | Continuation lines are needed when entering a multi-line construct. As an | 
 | 106 | example, take a look at this :keyword:`if` statement:: | 
 | 107 |  | 
 | 108 |    >>> the_world_is_flat = 1 | 
 | 109 |    >>> if the_world_is_flat: | 
 | 110 |    ...     print "Be careful not to fall off!" | 
| Georg Brandl | c62ef8b | 2009-01-03 20:55:06 +0000 | [diff] [blame] | 111 |    ... | 
| Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 112 |    Be careful not to fall off! | 
 | 113 |  | 
 | 114 |  | 
| Senthil Kumaran | 74012c1 | 2014-09-18 21:29:21 +0800 | [diff] [blame] | 115 | For more on interactive mode, see :ref:`tut-interac`. | 
 | 116 |  | 
 | 117 |  | 
| Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 118 | .. _tut-interp: | 
 | 119 |  | 
 | 120 | The Interpreter and Its Environment | 
 | 121 | =================================== | 
 | 122 |  | 
 | 123 |  | 
| Éric Araujo | ec464cf | 2011-07-29 11:35:27 +0200 | [diff] [blame] | 124 | .. _tut-source-encoding: | 
 | 125 |  | 
| Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 126 | Source Code Encoding | 
 | 127 | -------------------- | 
 | 128 |  | 
 | 129 | It is possible to use encodings different than ASCII in Python source files. The | 
 | 130 | best way to do it is to put one more special comment line right after the ``#!`` | 
 | 131 | line to define the source file encoding:: | 
 | 132 |  | 
| Georg Brandl | c62ef8b | 2009-01-03 20:55:06 +0000 | [diff] [blame] | 133 |    # -*- coding: encoding -*- | 
| Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 134 |  | 
 | 135 |  | 
 | 136 | With that declaration, all characters in the source file will be treated as | 
 | 137 | having the encoding *encoding*, and it will be possible to directly write | 
 | 138 | Unicode string literals in the selected encoding.  The list of possible | 
 | 139 | encodings can be found in the Python Library Reference, in the section on | 
 | 140 | :mod:`codecs`. | 
 | 141 |  | 
 | 142 | For example, to write Unicode literals including the Euro currency symbol, the | 
 | 143 | ISO-8859-15 encoding can be used, with the Euro symbol having the ordinal value | 
| Georg Brandl | 9642688 | 2013-10-06 11:24:48 +0200 | [diff] [blame] | 144 | 164.  This script, when saved in the ISO-8859-15 encoding, will print the value | 
| Georg Brandl | a44ec3f | 2015-01-14 08:26:30 +0100 | [diff] [blame] | 145 | 8364 (the Unicode code point corresponding to the Euro symbol) and then exit:: | 
| Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 146 |  | 
 | 147 |    # -*- coding: iso-8859-15 -*- | 
 | 148 |  | 
 | 149 |    currency = u"€" | 
 | 150 |    print ord(currency) | 
 | 151 |  | 
 | 152 | If your editor supports saving files as ``UTF-8`` with a UTF-8 *byte order mark* | 
 | 153 | (aka BOM), you can use that instead of an encoding declaration. IDLE supports | 
 | 154 | this capability if ``Options/General/Default Source Encoding/UTF-8`` is set. | 
 | 155 | Notice that this signature is not understood in older Python releases (2.2 and | 
 | 156 | earlier), and also not understood by the operating system for script files with | 
 | 157 | ``#!`` lines (only used on Unix systems). | 
 | 158 |  | 
 | 159 | By using UTF-8 (either through the signature or an encoding declaration), | 
 | 160 | characters of most languages in the world can be used simultaneously in string | 
 | 161 | literals and comments.  Using non-ASCII characters in identifiers is not | 
 | 162 | supported. To display all these characters properly, your editor must recognize | 
 | 163 | that the file is UTF-8, and it must use a font that supports all the characters | 
 | 164 | in the file. | 
 | 165 |  |