annotate Doc/tutorial/interpreter.rst @ 64166:d0e9febfa00a 3.1

Merged revisions 84172 via svnmerge from svn+ssh://pythondev@svn.python.org/python/branches/py3k ........ r84172 | martin.v.loewis | 2010-08-18 18:12:23 +0200 (Mi, 18 Aug 2010) | 2 lines Restore GIL in nis_cat in case of error. ........
author Martin v. Löwis <martin@v.loewis.de>
date Thu, 19 Aug 2010 09:03:03 +0000
parents c9bd62efe6ce
children 8084c668b637
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
42604
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
1 .. _tut-using:
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
2
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
3 ****************************
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
4 Using the Python Interpreter
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
5 ****************************
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
6
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
7
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
8 .. _tut-invoking:
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
9
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
10 Invoking the Interpreter
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
11 ========================
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
12
49946
5235dcda9076 Prep for Python 3.1!
Barry Warsaw <barry@python.org>
parents: 49330
diff changeset
13 The Python interpreter is usually installed as :file:`/usr/local/bin/python3.1`
48894
03e216dc8fec #3577: 3.0 is not installed as "python" on Unix.
Georg Brandl <georg@python.org>
parents: 47557
diff changeset
14 on those machines where it is available; putting :file:`/usr/local/bin` in your
42604
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
15 Unix shell's search path makes it possible to start it by typing the command ::
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
16
49946
5235dcda9076 Prep for Python 3.1!
Barry Warsaw <barry@python.org>
parents: 49330
diff changeset
17 python3.1
42604
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
18
48894
03e216dc8fec #3577: 3.0 is not installed as "python" on Unix.
Georg Brandl <georg@python.org>
parents: 47557
diff changeset
19 to the shell. [#]_ Since the choice of the directory where the interpreter lives
03e216dc8fec #3577: 3.0 is not installed as "python" on Unix.
Georg Brandl <georg@python.org>
parents: 47557
diff changeset
20 is an installation option, other places are possible; check with your local
03e216dc8fec #3577: 3.0 is not installed as "python" on Unix.
Georg Brandl <georg@python.org>
parents: 47557
diff changeset
21 Python guru or system administrator. (E.g., :file:`/usr/local/python` is a
03e216dc8fec #3577: 3.0 is not installed as "python" on Unix.
Georg Brandl <georg@python.org>
parents: 47557
diff changeset
22 popular alternative location.)
42604
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
23
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
24 On Windows machines, the Python installation is usually placed in
49946
5235dcda9076 Prep for Python 3.1!
Barry Warsaw <barry@python.org>
parents: 49330
diff changeset
25 :file:`C:\\Python31`, though you can change this when you're running the
42604
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
26 installer. To add this directory to your path, you can type the following
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
27 command into the command prompt in a DOS box::
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
28
49946
5235dcda9076 Prep for Python 3.1!
Barry Warsaw <barry@python.org>
parents: 49330
diff changeset
29 set path=%path%;C:\python31
42604
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
30
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
31 Typing an end-of-file character (:kbd:`Control-D` on Unix, :kbd:`Control-Z` on
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
32 Windows) at the primary prompt causes the interpreter to exit with a zero exit
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
33 status. If that doesn't work, you can exit the interpreter by typing the
56368
c9bd62efe6ce Merged revisions 75231 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 56062
diff changeset
34 following command: ``quit()``.
42604
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
35
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
36 The interpreter's line-editing features usually aren't very sophisticated. On
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
37 Unix, whoever installed the interpreter may have enabled support for the GNU
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
38 readline library, which adds more elaborate interactive editing and history
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
39 features. Perhaps the quickest check to see whether command line editing is
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
40 supported is typing Control-P to the first Python prompt you get. If it beeps,
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
41 you have command line editing; see Appendix :ref:`tut-interacting` for an
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
42 introduction to the keys. If nothing appears to happen, or if ``^P`` is echoed,
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
43 command line editing isn't available; you'll only be able to use backspace to
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
44 remove characters from the current line.
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
45
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
46 The interpreter operates somewhat like the Unix shell: when called with standard
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
47 input connected to a tty device, it reads and executes commands interactively;
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
48 when called with a file name argument or with a file as standard input, it reads
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
49 and executes a *script* from that file.
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
50
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
51 A second way of starting the interpreter is ``python -c command [arg] ...``,
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
52 which executes the statement(s) in *command*, analogous to the shell's
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
53 :option:`-c` option. Since Python statements often contain spaces or other
47557
98827a21e352 Merged revisions 63724,63726,63732,63744,63754-63755,63757-63758,63760,63775,63781-63782,63787,63805-63808,63818-63819,63823-63824 via svnmerge from
Georg Brandl <georg@python.org>
parents: 44834
diff changeset
54 characters that are special to the shell, it is usually advised to quote
98827a21e352 Merged revisions 63724,63726,63732,63744,63754-63755,63757-63758,63760,63775,63781-63782,63787,63805-63808,63818-63819,63823-63824 via svnmerge from
Georg Brandl <georg@python.org>
parents: 44834
diff changeset
55 *command* in its entirety with single quotes.
42604
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
56
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
57 Some Python modules are also useful as scripts. These can be invoked using
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
58 ``python -m module [arg] ...``, which executes the source file for *module* as
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
59 if you had spelled out its full name on the command line.
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
60
48894
03e216dc8fec #3577: 3.0 is not installed as "python" on Unix.
Georg Brandl <georg@python.org>
parents: 47557
diff changeset
61 Note that there is a difference between ``python file`` and ``python
03e216dc8fec #3577: 3.0 is not installed as "python" on Unix.
Georg Brandl <georg@python.org>
parents: 47557
diff changeset
62 <file``. In the latter case, input requests from the program, such as calling
42604
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
63 ``sys.stdin.read()``, are satisfied from *file*. Since this file has already
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
64 been read until the end by the parser before the program starts executing, the
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
65 program will encounter end-of-file immediately. In the former case (which is
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
66 usually what you want) they are satisfied from whatever file or device is
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
67 connected to standard input of the Python interpreter.
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
68
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
69 When a script file is used, it is sometimes useful to be able to run the script
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
70 and enter interactive mode afterwards. This can be done by passing :option:`-i`
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
71 before the script. (This does not work if the script is read from standard
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
72 input, for the same reason as explained in the previous paragraph.)
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
73
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
74
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
75 .. _tut-argpassing:
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
76
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
77 Argument Passing
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
78 ----------------
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
79
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
80 When known to the interpreter, the script name and additional arguments
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
81 thereafter are passed to the script in the variable ``sys.argv``, which is a
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
82 list of strings. Its length is at least one; when no script and no arguments
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
83 are given, ``sys.argv[0]`` is an empty string. When the script name is given as
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
84 ``'-'`` (meaning standard input), ``sys.argv[0]`` is set to ``'-'``. When
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
85 :option:`-c` *command* is used, ``sys.argv[0]`` is set to ``'-c'``. When
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
86 :option:`-m` *module* is used, ``sys.argv[0]`` is set to the full name of the
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
87 located module. Options found after :option:`-c` *command* or :option:`-m`
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
88 *module* are not consumed by the Python interpreter's option processing but
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
89 left in ``sys.argv`` for the command or module to handle.
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
90
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
91
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
92 .. _tut-interactive:
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
93
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
94 Interactive Mode
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
95 ----------------
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
96
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
97 When commands are read from a tty, the interpreter is said to be in *interactive
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
98 mode*. In this mode it prompts for the next command with the *primary prompt*,
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
99 usually three greater-than signs (``>>>``); for continuation lines it prompts
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
100 with the *secondary prompt*, by default three dots (``...``). The interpreter
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
101 prints a welcome message stating its version number and a copyright notice
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
102 before printing the first prompt::
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
103
49946
5235dcda9076 Prep for Python 3.1!
Barry Warsaw <barry@python.org>
parents: 49330
diff changeset
104 $ python3.1
56062
9817915ebdeb #6935: update version.
Georg Brandl <georg@python.org>
parents: 50514
diff changeset
105 Python 3.1 (py3k, Sep 12 2007, 12:21:02)
43449
7fcd9945d1bf #1211, #1212, #1213: py3k fixes to the tutorial.
Georg Brandl <georg@python.org>
parents: 43219
diff changeset
106 [GCC 3.4.6 20060404 (Red Hat 3.4.6-8)] on linux2
7fcd9945d1bf #1211, #1212, #1213: py3k fixes to the tutorial.
Georg Brandl <georg@python.org>
parents: 43219
diff changeset
107 Type "help", "copyright", "credits" or "license" for more information.
42604
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
108 >>>
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
109
56062
9817915ebdeb #6935: update version.
Georg Brandl <georg@python.org>
parents: 50514
diff changeset
110 .. XXX update for new releases
43449
7fcd9945d1bf #1211, #1212, #1213: py3k fixes to the tutorial.
Georg Brandl <georg@python.org>
parents: 43219
diff changeset
111
42604
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
112 Continuation lines are needed when entering a multi-line construct. As an
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
113 example, take a look at this :keyword:`if` statement::
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
114
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
115 >>> the_world_is_flat = 1
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
116 >>> if the_world_is_flat:
43219
655071bf6c45 Convert all print statements in the docs.
Georg Brandl <georg@python.org>
parents: 42604
diff changeset
117 ... print("Be careful not to fall off!")
50514
c3d3b760d8bf Remove trailing whitespace.
Georg Brandl <georg@python.org>
parents: 49946
diff changeset
118 ...
42604
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
119 Be careful not to fall off!
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
120
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
121
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
122 .. _tut-interp:
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
123
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
124 The Interpreter and Its Environment
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
125 ===================================
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
126
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
127
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
128 .. _tut-error:
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
129
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
130 Error Handling
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
131 --------------
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
132
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
133 When an error occurs, the interpreter prints an error message and a stack trace.
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
134 In interactive mode, it then returns to the primary prompt; when input came from
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
135 a file, it exits with a nonzero exit status after printing the stack trace.
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
136 (Exceptions handled by an :keyword:`except` clause in a :keyword:`try` statement
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
137 are not errors in this context.) Some errors are unconditionally fatal and
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
138 cause an exit with a nonzero exit; this applies to internal inconsistencies and
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
139 some cases of running out of memory. All error messages are written to the
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
140 standard error stream; normal output from executed commands is written to
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
141 standard output.
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
142
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
143 Typing the interrupt character (usually Control-C or DEL) to the primary or
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
144 secondary prompt cancels the input and returns to the primary prompt. [#]_
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
145 Typing an interrupt while a command is executing raises the
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
146 :exc:`KeyboardInterrupt` exception, which may be handled by a :keyword:`try`
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
147 statement.
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
148
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
149
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
150 .. _tut-scripts:
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
151
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
152 Executable Python Scripts
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
153 -------------------------
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
154
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
155 On BSD'ish Unix systems, Python scripts can be made directly executable, like
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
156 shell scripts, by putting the line ::
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
157
49946
5235dcda9076 Prep for Python 3.1!
Barry Warsaw <barry@python.org>
parents: 49330
diff changeset
158 #! /usr/bin/env python3.1
42604
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
159
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
160 (assuming that the interpreter is on the user's :envvar:`PATH`) at the beginning
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
161 of the script and giving the file an executable mode. The ``#!`` must be the
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
162 first two characters of the file. On some platforms, this first line must end
49186
47db62bf6aa7 Merged revisions 66452 via svnmerge from
Georg Brandl <georg@python.org>
parents: 48894
diff changeset
163 with a Unix-style line ending (``'\n'``), not a Windows (``'\r\n'``) line
47db62bf6aa7 Merged revisions 66452 via svnmerge from
Georg Brandl <georg@python.org>
parents: 48894
diff changeset
164 ending. Note that the hash, or pound, character, ``'#'``, is used to start a
47db62bf6aa7 Merged revisions 66452 via svnmerge from
Georg Brandl <georg@python.org>
parents: 48894
diff changeset
165 comment in Python.
42604
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
166
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
167 The script can be given an executable mode, or permission, using the
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
168 :program:`chmod` command::
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
169
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
170 $ chmod +x myscript.py
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
171
44834
ed33da2e74e7 Merged revisions 60143-60149 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44396
diff changeset
172 On Windows systems, there is no notion of an "executable mode". The Python
ed33da2e74e7 Merged revisions 60143-60149 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44396
diff changeset
173 installer automatically associates ``.py`` files with ``python.exe`` so that
ed33da2e74e7 Merged revisions 60143-60149 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44396
diff changeset
174 a double-click on a Python file will run it as a script. The extension can
ed33da2e74e7 Merged revisions 60143-60149 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44396
diff changeset
175 also be ``.pyw``, in that case, the console window that normally appears is
ed33da2e74e7 Merged revisions 60143-60149 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44396
diff changeset
176 suppressed.
ed33da2e74e7 Merged revisions 60143-60149 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44396
diff changeset
177
42604
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
178
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
179 Source Code Encoding
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
180 --------------------
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
181
43449
7fcd9945d1bf #1211, #1212, #1213: py3k fixes to the tutorial.
Georg Brandl <georg@python.org>
parents: 43219
diff changeset
182 By default, Python source files are treated as encoded in UTF-8. In that
7fcd9945d1bf #1211, #1212, #1213: py3k fixes to the tutorial.
Georg Brandl <georg@python.org>
parents: 43219
diff changeset
183 encoding, characters of most languages in the world can be used simultaneously
7fcd9945d1bf #1211, #1212, #1213: py3k fixes to the tutorial.
Georg Brandl <georg@python.org>
parents: 43219
diff changeset
184 in string literals, identifiers and comments --- although the standard library
7fcd9945d1bf #1211, #1212, #1213: py3k fixes to the tutorial.
Georg Brandl <georg@python.org>
parents: 43219
diff changeset
185 only uses ASCII characters for identifiers, a convention that any portable code
7fcd9945d1bf #1211, #1212, #1213: py3k fixes to the tutorial.
Georg Brandl <georg@python.org>
parents: 43219
diff changeset
186 should follow. To display all these characters properly, your editor must
7fcd9945d1bf #1211, #1212, #1213: py3k fixes to the tutorial.
Georg Brandl <georg@python.org>
parents: 43219
diff changeset
187 recognize that the file is UTF-8, and it must use a font that supports all the
7fcd9945d1bf #1211, #1212, #1213: py3k fixes to the tutorial.
Georg Brandl <georg@python.org>
parents: 43219
diff changeset
188 characters in the file.
43219
655071bf6c45 Convert all print statements in the docs.
Georg Brandl <georg@python.org>
parents: 42604
diff changeset
189
43449
7fcd9945d1bf #1211, #1212, #1213: py3k fixes to the tutorial.
Georg Brandl <georg@python.org>
parents: 43219
diff changeset
190 It is also possible to specify a different encoding for source files. In order
7fcd9945d1bf #1211, #1212, #1213: py3k fixes to the tutorial.
Georg Brandl <georg@python.org>
parents: 43219
diff changeset
191 to do this, put one more special comment line right after the ``#!`` line to
7fcd9945d1bf #1211, #1212, #1213: py3k fixes to the tutorial.
Georg Brandl <georg@python.org>
parents: 43219
diff changeset
192 define the source file encoding::
42604
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
193
50514
c3d3b760d8bf Remove trailing whitespace.
Georg Brandl <georg@python.org>
parents: 49946
diff changeset
194 # -*- coding: encoding -*-
42604
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
195
43449
7fcd9945d1bf #1211, #1212, #1213: py3k fixes to the tutorial.
Georg Brandl <georg@python.org>
parents: 43219
diff changeset
196 With that declaration, everything in the source file will be treated as having
7fcd9945d1bf #1211, #1212, #1213: py3k fixes to the tutorial.
Georg Brandl <georg@python.org>
parents: 43219
diff changeset
197 the encoding *encoding* instead of UTF-8. The list of possible encodings can be
7fcd9945d1bf #1211, #1212, #1213: py3k fixes to the tutorial.
Georg Brandl <georg@python.org>
parents: 43219
diff changeset
198 found in the Python Library Reference, in the section on :mod:`codecs`.
42604
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
199
43449
7fcd9945d1bf #1211, #1212, #1213: py3k fixes to the tutorial.
Georg Brandl <georg@python.org>
parents: 43219
diff changeset
200 For example, if your editor of choice does not support UTF-8 encoded files and
7fcd9945d1bf #1211, #1212, #1213: py3k fixes to the tutorial.
Georg Brandl <georg@python.org>
parents: 43219
diff changeset
201 insists on using some other encoding, say Windows-1252, you can write::
42604
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
202
43449
7fcd9945d1bf #1211, #1212, #1213: py3k fixes to the tutorial.
Georg Brandl <georg@python.org>
parents: 43219
diff changeset
203 # -*- coding: cp-1252 -*-
42604
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
204
43449
7fcd9945d1bf #1211, #1212, #1213: py3k fixes to the tutorial.
Georg Brandl <georg@python.org>
parents: 43219
diff changeset
205 and still use all characters in the Windows-1252 character set in the source
7fcd9945d1bf #1211, #1212, #1213: py3k fixes to the tutorial.
Georg Brandl <georg@python.org>
parents: 43219
diff changeset
206 files. The special encoding comment must be in the *first or second* line
7fcd9945d1bf #1211, #1212, #1213: py3k fixes to the tutorial.
Georg Brandl <georg@python.org>
parents: 43219
diff changeset
207 within the file.
42604
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
208
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
209
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
210 .. _tut-startup:
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
211
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
212 The Interactive Startup File
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
213 ----------------------------
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
214
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
215 When you use Python interactively, it is frequently handy to have some standard
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
216 commands executed every time the interpreter is started. You can do this by
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
217 setting an environment variable named :envvar:`PYTHONSTARTUP` to the name of a
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
218 file containing your start-up commands. This is similar to the :file:`.profile`
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
219 feature of the Unix shells.
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
220
44396
6556f3f22c19 Merged revisions 59605-59624 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43449
diff changeset
221 .. XXX This should probably be dumped in an appendix, since most people
6556f3f22c19 Merged revisions 59605-59624 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43449
diff changeset
222 don't use Python interactively in non-trivial ways.
42604
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
223
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
224 This file is only read in interactive sessions, not when Python reads commands
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
225 from a script, and not when :file:`/dev/tty` is given as the explicit source of
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
226 commands (which otherwise behaves like an interactive session). It is executed
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
227 in the same namespace where interactive commands are executed, so that objects
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
228 that it defines or imports can be used without qualification in the interactive
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
229 session. You can also change the prompts ``sys.ps1`` and ``sys.ps2`` in this
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
230 file.
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
231
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
232 If you want to read an additional start-up file from the current directory, you
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
233 can program this in the global start-up file using code like ``if
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
234 os.path.isfile('.pythonrc.py'): exec(open('.pythonrc.py').read())``.
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
235 If you want to use the startup file in a script, you must do this explicitly
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
236 in the script::
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
237
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
238 import os
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
239 filename = os.environ.get('PYTHONSTARTUP')
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
240 if filename and os.path.isfile(filename):
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
241 exec(open(filename).read())
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
242
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
243
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
244 .. rubric:: Footnotes
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
245
56062
9817915ebdeb #6935: update version.
Georg Brandl <georg@python.org>
parents: 50514
diff changeset
246 .. [#] On Unix, the Python 3.x interpreter is by default not installed with the
48894
03e216dc8fec #3577: 3.0 is not installed as "python" on Unix.
Georg Brandl <georg@python.org>
parents: 47557
diff changeset
247 executable named ``python``, so that it does not conflict with a
03e216dc8fec #3577: 3.0 is not installed as "python" on Unix.
Georg Brandl <georg@python.org>
parents: 47557
diff changeset
248 simultaneously installed Python 2.x executable.
03e216dc8fec #3577: 3.0 is not installed as "python" on Unix.
Georg Brandl <georg@python.org>
parents: 47557
diff changeset
249
42604
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
250 .. [#] A problem with the GNU Readline package may prevent this.
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
251