Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +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 | |
Georg Brandl | 3ebb6b3 | 2011-02-20 10:37:07 +0000 | [diff] [blame] | 13 | The Python interpreter is usually installed as :file:`/usr/local/bin/python3.3` |
Georg Brandl | 3db38ce | 2008-08-30 09:58:30 +0000 | [diff] [blame] | 14 | on those machines where it is available; putting :file:`/usr/local/bin` in your |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 15 | Unix shell's search path makes it possible to start it by typing the command :: |
| 16 | |
Georg Brandl | 3ebb6b3 | 2011-02-20 10:37:07 +0000 | [diff] [blame] | 17 | python3.3 |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 18 | |
Georg Brandl | 3db38ce | 2008-08-30 09:58:30 +0000 | [diff] [blame] | 19 | to the shell. [#]_ Since the choice of the directory where the interpreter lives |
| 20 | is an installation option, other places are possible; check with your local |
| 21 | Python guru or system administrator. (E.g., :file:`/usr/local/python` is a |
| 22 | popular alternative location.) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 23 | |
| 24 | On Windows machines, the Python installation is usually placed in |
Georg Brandl | 3ebb6b3 | 2011-02-20 10:37:07 +0000 | [diff] [blame] | 25 | :file:`C:\\Python33`, though you can change this when you're running the |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +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 | 3ebb6b3 | 2011-02-20 10:37:07 +0000 | [diff] [blame] | 29 | set path=%path%;C:\python33 |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +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 |
Benjamin Peterson | 4ac9ce4 | 2009-10-04 14:49:41 +0000 | [diff] [blame] | 34 | following command: ``quit()``. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +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 |
| 40 | supported is typing Control-P to the first Python prompt you get. If it beeps, |
| 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 | f08a9dd | 2008-06-10 16:57:31 +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 | 116aa62 | 2007-08-15 14:28:22 +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 | 116aa62 | 2007-08-15 14:28:22 +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 | 69e59a1 | 2011-10-31 17:15:39 +0100 | [diff] [blame] | 63 | before the script. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 64 | |
| 65 | |
| 66 | .. _tut-argpassing: |
| 67 | |
| 68 | Argument Passing |
| 69 | ---------------- |
| 70 | |
| 71 | When known to the interpreter, the script name and additional arguments |
R. David Murray | a396463 | 2010-12-17 16:11:40 +0000 | [diff] [blame] | 72 | thereafter are turned into a list of strings and assigned to the ``argv`` |
| 73 | variable in the ``sys`` module. You can access this list by executing ``import |
| 74 | sys``. The length of the list is at least one; when no script and no arguments |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 75 | are given, ``sys.argv[0]`` is an empty string. When the script name is given as |
| 76 | ``'-'`` (meaning standard input), ``sys.argv[0]`` is set to ``'-'``. When |
| 77 | :option:`-c` *command* is used, ``sys.argv[0]`` is set to ``'-c'``. When |
| 78 | :option:`-m` *module* is used, ``sys.argv[0]`` is set to the full name of the |
| 79 | located module. Options found after :option:`-c` *command* or :option:`-m` |
| 80 | *module* are not consumed by the Python interpreter's option processing but |
| 81 | left in ``sys.argv`` for the command or module to handle. |
| 82 | |
| 83 | |
| 84 | .. _tut-interactive: |
| 85 | |
| 86 | Interactive Mode |
| 87 | ---------------- |
| 88 | |
| 89 | When commands are read from a tty, the interpreter is said to be in *interactive |
| 90 | mode*. In this mode it prompts for the next command with the *primary prompt*, |
| 91 | usually three greater-than signs (``>>>``); for continuation lines it prompts |
| 92 | with the *secondary prompt*, by default three dots (``...``). The interpreter |
| 93 | prints a welcome message stating its version number and a copyright notice |
| 94 | before printing the first prompt:: |
| 95 | |
Georg Brandl | 3ebb6b3 | 2011-02-20 10:37:07 +0000 | [diff] [blame] | 96 | $ python3.3 |
| 97 | Python 3.3 (py3k, Sep 12 2007, 12:21:02) |
Georg Brandl | 2d2590d | 2007-09-28 13:13:35 +0000 | [diff] [blame] | 98 | [GCC 3.4.6 20060404 (Red Hat 3.4.6-8)] on linux2 |
| 99 | Type "help", "copyright", "credits" or "license" for more information. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 100 | >>> |
| 101 | |
Georg Brandl | a17487b | 2009-09-18 07:27:51 +0000 | [diff] [blame] | 102 | .. XXX update for new releases |
Georg Brandl | 2d2590d | 2007-09-28 13:13:35 +0000 | [diff] [blame] | 103 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 104 | Continuation lines are needed when entering a multi-line construct. As an |
| 105 | example, take a look at this :keyword:`if` statement:: |
| 106 | |
| 107 | >>> the_world_is_flat = 1 |
| 108 | >>> if the_world_is_flat: |
Georg Brandl | 6911e3c | 2007-09-04 07:15:32 +0000 | [diff] [blame] | 109 | ... print("Be careful not to fall off!") |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 110 | ... |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 111 | Be careful not to fall off! |
| 112 | |
| 113 | |
| 114 | .. _tut-interp: |
| 115 | |
| 116 | The Interpreter and Its Environment |
| 117 | =================================== |
| 118 | |
| 119 | |
| 120 | .. _tut-error: |
| 121 | |
| 122 | Error Handling |
| 123 | -------------- |
| 124 | |
| 125 | When an error occurs, the interpreter prints an error message and a stack trace. |
| 126 | In interactive mode, it then returns to the primary prompt; when input came from |
| 127 | a file, it exits with a nonzero exit status after printing the stack trace. |
| 128 | (Exceptions handled by an :keyword:`except` clause in a :keyword:`try` statement |
| 129 | are not errors in this context.) Some errors are unconditionally fatal and |
| 130 | cause an exit with a nonzero exit; this applies to internal inconsistencies and |
| 131 | some cases of running out of memory. All error messages are written to the |
| 132 | standard error stream; normal output from executed commands is written to |
| 133 | standard output. |
| 134 | |
| 135 | Typing the interrupt character (usually Control-C or DEL) to the primary or |
| 136 | secondary prompt cancels the input and returns to the primary prompt. [#]_ |
| 137 | Typing an interrupt while a command is executing raises the |
| 138 | :exc:`KeyboardInterrupt` exception, which may be handled by a :keyword:`try` |
| 139 | statement. |
| 140 | |
| 141 | |
| 142 | .. _tut-scripts: |
| 143 | |
| 144 | Executable Python Scripts |
| 145 | ------------------------- |
| 146 | |
| 147 | On BSD'ish Unix systems, Python scripts can be made directly executable, like |
| 148 | shell scripts, by putting the line :: |
| 149 | |
Georg Brandl | 3ebb6b3 | 2011-02-20 10:37:07 +0000 | [diff] [blame] | 150 | #! /usr/bin/env python3.3 |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 151 | |
| 152 | (assuming that the interpreter is on the user's :envvar:`PATH`) at the beginning |
| 153 | of the script and giving the file an executable mode. The ``#!`` must be the |
| 154 | first two characters of the file. On some platforms, this first line must end |
Georg Brandl | c575c90 | 2008-09-13 17:46:05 +0000 | [diff] [blame] | 155 | with a Unix-style line ending (``'\n'``), not a Windows (``'\r\n'``) line |
| 156 | ending. Note that the hash, or pound, character, ``'#'``, is used to start a |
| 157 | comment in Python. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 158 | |
| 159 | The script can be given an executable mode, or permission, using the |
| 160 | :program:`chmod` command:: |
| 161 | |
| 162 | $ chmod +x myscript.py |
| 163 | |
Christian Heimes | e1c9811 | 2008-01-21 11:20:28 +0000 | [diff] [blame] | 164 | On Windows systems, there is no notion of an "executable mode". The Python |
| 165 | installer automatically associates ``.py`` files with ``python.exe`` so that |
| 166 | a double-click on a Python file will run it as a script. The extension can |
| 167 | also be ``.pyw``, in that case, the console window that normally appears is |
| 168 | suppressed. |
| 169 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 170 | |
Éric Araujo | 9fbfe15 | 2011-06-11 10:34:19 +0200 | [diff] [blame] | 171 | .. _tut-source-encoding: |
| 172 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 173 | Source Code Encoding |
| 174 | -------------------- |
| 175 | |
Georg Brandl | 2d2590d | 2007-09-28 13:13:35 +0000 | [diff] [blame] | 176 | By default, Python source files are treated as encoded in UTF-8. In that |
| 177 | encoding, characters of most languages in the world can be used simultaneously |
| 178 | in string literals, identifiers and comments --- although the standard library |
| 179 | only uses ASCII characters for identifiers, a convention that any portable code |
| 180 | should follow. To display all these characters properly, your editor must |
| 181 | recognize that the file is UTF-8, and it must use a font that supports all the |
| 182 | characters in the file. |
Georg Brandl | 6911e3c | 2007-09-04 07:15:32 +0000 | [diff] [blame] | 183 | |
Georg Brandl | 2d2590d | 2007-09-28 13:13:35 +0000 | [diff] [blame] | 184 | It is also possible to specify a different encoding for source files. In order |
| 185 | to do this, put one more special comment line right after the ``#!`` line to |
| 186 | define the source file encoding:: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 187 | |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 188 | # -*- coding: encoding -*- |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 189 | |
Georg Brandl | 2d2590d | 2007-09-28 13:13:35 +0000 | [diff] [blame] | 190 | With that declaration, everything in the source file will be treated as having |
| 191 | the encoding *encoding* instead of UTF-8. The list of possible encodings can be |
| 192 | found in the Python Library Reference, in the section on :mod:`codecs`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 193 | |
Georg Brandl | 2d2590d | 2007-09-28 13:13:35 +0000 | [diff] [blame] | 194 | For example, if your editor of choice does not support UTF-8 encoded files and |
| 195 | insists on using some other encoding, say Windows-1252, you can write:: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 196 | |
Georg Brandl | 2d2590d | 2007-09-28 13:13:35 +0000 | [diff] [blame] | 197 | # -*- coding: cp-1252 -*- |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 198 | |
Georg Brandl | 2d2590d | 2007-09-28 13:13:35 +0000 | [diff] [blame] | 199 | and still use all characters in the Windows-1252 character set in the source |
| 200 | files. The special encoding comment must be in the *first or second* line |
| 201 | within the file. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 202 | |
| 203 | |
| 204 | .. _tut-startup: |
| 205 | |
| 206 | The Interactive Startup File |
| 207 | ---------------------------- |
| 208 | |
| 209 | When you use Python interactively, it is frequently handy to have some standard |
| 210 | commands executed every time the interpreter is started. You can do this by |
| 211 | setting an environment variable named :envvar:`PYTHONSTARTUP` to the name of a |
| 212 | file containing your start-up commands. This is similar to the :file:`.profile` |
| 213 | feature of the Unix shells. |
| 214 | |
Christian Heimes | 5b5e81c | 2007-12-31 16:14:33 +0000 | [diff] [blame] | 215 | .. XXX This should probably be dumped in an appendix, since most people |
| 216 | don't use Python interactively in non-trivial ways. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 217 | |
| 218 | This file is only read in interactive sessions, not when Python reads commands |
| 219 | from a script, and not when :file:`/dev/tty` is given as the explicit source of |
| 220 | commands (which otherwise behaves like an interactive session). It is executed |
| 221 | in the same namespace where interactive commands are executed, so that objects |
| 222 | that it defines or imports can be used without qualification in the interactive |
| 223 | session. You can also change the prompts ``sys.ps1`` and ``sys.ps2`` in this |
| 224 | file. |
| 225 | |
| 226 | If you want to read an additional start-up file from the current directory, you |
| 227 | can program this in the global start-up file using code like ``if |
| 228 | os.path.isfile('.pythonrc.py'): exec(open('.pythonrc.py').read())``. |
| 229 | If you want to use the startup file in a script, you must do this explicitly |
| 230 | in the script:: |
| 231 | |
| 232 | import os |
| 233 | filename = os.environ.get('PYTHONSTARTUP') |
| 234 | if filename and os.path.isfile(filename): |
| 235 | exec(open(filename).read()) |
| 236 | |
| 237 | |
Éric Araujo | de4f05b | 2011-08-06 01:51:07 +0200 | [diff] [blame] | 238 | .. _tut-customize: |
| 239 | |
| 240 | The Customization Modules |
| 241 | ------------------------- |
| 242 | |
| 243 | Python provides two hooks to let you customize it: :mod:`sitecustomize` and |
| 244 | :mod:`usercustomize`. To see how it works, you need first to find the location |
| 245 | of your user site-packages directory. Start Python and run this code: |
| 246 | |
| 247 | >>> import site |
| 248 | >>> site.getusersitepackages() |
| 249 | '/home/user/.local/lib/python3.2/site-packages' |
| 250 | |
| 251 | Now you can create a file named :file:`usercustomize.py` in that directory and |
| 252 | put anything you want in it. It will affect every invocation of Python, unless |
| 253 | it is started with the :option:`-s` option to disable the automatic import. |
| 254 | |
| 255 | :mod:`sitecustomize` works in the same way, but is typically created by an |
| 256 | administrator of the computer in the global site-packages directory, and is |
| 257 | imported before :mod:`usercustomize`. See the documentation of the :mod:`site` |
| 258 | module for more details. |
| 259 | |
| 260 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 261 | .. rubric:: Footnotes |
| 262 | |
Georg Brandl | a17487b | 2009-09-18 07:27:51 +0000 | [diff] [blame] | 263 | .. [#] On Unix, the Python 3.x interpreter is by default not installed with the |
Georg Brandl | 3db38ce | 2008-08-30 09:58:30 +0000 | [diff] [blame] | 264 | executable named ``python``, so that it does not conflict with a |
| 265 | simultaneously installed Python 2.x executable. |
| 266 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 267 | .. [#] A problem with the GNU Readline package may prevent this. |