annotate Doc/tutorial/interpreter.rst @ 74135:9c19df6c8ea0 3.2

News entry for Issue12798
author Senthil Kumaran <senthil@uthcode.com>
date Fri, 23 Dec 2011 11:11:21 +0800
parents 7a2877d12d52
children d2df5bc89fc9 6b5a58836dae
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
54872
a24f09606b83 bump to 3.2a0
Benjamin Peterson <benjamin@python.org>
parents: 50514
diff changeset
13 The Python interpreter is usually installed as :file:`/usr/local/bin/python3.2`
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
54872
a24f09606b83 bump to 3.2a0
Benjamin Peterson <benjamin@python.org>
parents: 50514
diff changeset
17 python3.2
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
54872
a24f09606b83 bump to 3.2a0
Benjamin Peterson <benjamin@python.org>
parents: 50514
diff changeset
25 :file:`C:\\Python32`, 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
54872
a24f09606b83 bump to 3.2a0
Benjamin Peterson <benjamin@python.org>
parents: 50514
diff changeset
29 set path=%path%;C:\python32
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
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
61 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
62 and enter interactive mode afterwards. This can be done by passing :option:`-i`
73251
7a2877d12d52 remove confusing paragraph (as part of r87523); thanks to AJ Hill from docs@
Sandro Tosi <sandro.tosi@gmail.com>
parents: 71778
diff changeset
63 before the script.
42604
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
64
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
65
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
66 .. _tut-argpassing:
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
67
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
68 Argument Passing
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
69 ----------------
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
70
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
71 When known to the interpreter, the script name and additional arguments
67917
8084c668b637 Merged revisions 87136,87221,87256,87337-87338,87571,87839,88164 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 56368
diff changeset
72 thereafter are turned into a list of strings and assigned to the ``argv``
8084c668b637 Merged revisions 87136,87221,87256,87337-87338,87571,87839,88164 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 56368
diff changeset
73 variable in the ``sys`` module. You can access this list by executing ``import
8084c668b637 Merged revisions 87136,87221,87256,87337-87338,87571,87839,88164 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 56368
diff changeset
74 sys``. The length of the list is at least one; when no script and no arguments
42604
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
75 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
76 ``'-'`` (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
77 :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
78 :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
79 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
80 *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
81 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
82
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
83
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
84 .. _tut-interactive:
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
85
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
86 Interactive Mode
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
87 ----------------
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
88
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
89 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
90 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
91 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
92 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
93 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
94 before printing the first prompt::
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
95
56061
5a1e879832fe #6935: update version.
Georg Brandl <georg@python.org>
parents: 54872
diff changeset
96 $ python3.2
5a1e879832fe #6935: update version.
Georg Brandl <georg@python.org>
parents: 54872
diff changeset
97 Python 3.2 (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
98 [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
99 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
100 >>>
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
101
56062
9817915ebdeb #6935: update version.
Georg Brandl <georg@python.org>
parents: 50514
diff changeset
102 .. XXX update for new releases
43449
7fcd9945d1bf #1211, #1212, #1213: py3k fixes to the tutorial.
Georg Brandl <georg@python.org>
parents: 43219
diff changeset
103
42604
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
104 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
105 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
106
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
107 >>> the_world_is_flat = 1
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
108 >>> if the_world_is_flat:
43219
655071bf6c45 Convert all print statements in the docs.
Georg Brandl <georg@python.org>
parents: 42604
diff changeset
109 ... print("Be careful not to fall off!")
50514
c3d3b760d8bf Remove trailing whitespace.
Georg Brandl <georg@python.org>
parents: 49946
diff changeset
110 ...
42604
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
111 Be careful not to fall off!
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
112
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
113
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
114 .. _tut-interp:
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
115
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
116 The Interpreter and Its Environment
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
117 ===================================
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
118
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
119
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
120 .. _tut-error:
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 Error Handling
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
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
125 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
126 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
127 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
128 (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
129 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
130 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
131 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
132 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
133 standard output.
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
134
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
135 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
136 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
137 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
138 :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
139 statement.
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
140
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
141
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
142 .. _tut-scripts:
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
143
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
144 Executable Python Scripts
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
145 -------------------------
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
146
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
147 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
148 shell scripts, by putting the line ::
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
149
60758
cea38ef60700 Update the python version in the interpreter tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 56367
diff changeset
150 #! /usr/bin/env python3.2
42604
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 (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
153 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
154 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
155 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
156 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
157 comment in Python.
42604
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
158
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
159 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
160 :program:`chmod` command::
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
161
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
162 $ chmod +x myscript.py
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
163
44834
ed33da2e74e7 Merged revisions 60143-60149 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44396
diff changeset
164 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
165 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
166 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
167 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
168 suppressed.
ed33da2e74e7 Merged revisions 60143-60149 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44396
diff changeset
169
42604
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
170
70795
9bfaa723a194 Add missing reST target to one heading in the tutorial
Éric Araujo <merwok@netwok.org>
parents: 68257
diff changeset
171 .. _tut-source-encoding:
9bfaa723a194 Add missing reST target to one heading in the tutorial
Éric Araujo <merwok@netwok.org>
parents: 68257
diff changeset
172
42604
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
173 Source Code Encoding
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
174 --------------------
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
175
43449
7fcd9945d1bf #1211, #1212, #1213: py3k fixes to the tutorial.
Georg Brandl <georg@python.org>
parents: 43219
diff changeset
176 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
177 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
178 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
179 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
180 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
181 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
182 characters in the file.
43219
655071bf6c45 Convert all print statements in the docs.
Georg Brandl <georg@python.org>
parents: 42604
diff changeset
183
43449
7fcd9945d1bf #1211, #1212, #1213: py3k fixes to the tutorial.
Georg Brandl <georg@python.org>
parents: 43219
diff changeset
184 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
185 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
186 define the source file encoding::
42604
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
187
50514
c3d3b760d8bf Remove trailing whitespace.
Georg Brandl <georg@python.org>
parents: 49946
diff changeset
188 # -*- coding: encoding -*-
42604
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
189
43449
7fcd9945d1bf #1211, #1212, #1213: py3k fixes to the tutorial.
Georg Brandl <georg@python.org>
parents: 43219
diff changeset
190 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
191 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
192 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
193
43449
7fcd9945d1bf #1211, #1212, #1213: py3k fixes to the tutorial.
Georg Brandl <georg@python.org>
parents: 43219
diff changeset
194 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
195 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
196
43449
7fcd9945d1bf #1211, #1212, #1213: py3k fixes to the tutorial.
Georg Brandl <georg@python.org>
parents: 43219
diff changeset
197 # -*- coding: cp-1252 -*-
42604
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
198
43449
7fcd9945d1bf #1211, #1212, #1213: py3k fixes to the tutorial.
Georg Brandl <georg@python.org>
parents: 43219
diff changeset
199 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
200 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
201 within the file.
42604
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
202
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
203
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
204 .. _tut-startup:
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
205
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
206 The Interactive Startup File
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
207 ----------------------------
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 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
210 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
211 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
212 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
213 feature of the Unix shells.
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
214
44396
6556f3f22c19 Merged revisions 59605-59624 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43449
diff changeset
215 .. 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
216 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
217
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
218 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
219 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
220 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
221 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
222 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
223 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
224 file.
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
225
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
226 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
227 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
228 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
229 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
230 in the script::
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 import os
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
233 filename = os.environ.get('PYTHONSTARTUP')
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
234 if filename and os.path.isfile(filename):
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
235 exec(open(filename).read())
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
236
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
237
71778
74179a79d673 Improve documentation for PEP 370 support in site module (#8617).
Éric Araujo <merwok@netwok.org>
parents: 70795
diff changeset
238 .. _tut-customize:
74179a79d673 Improve documentation for PEP 370 support in site module (#8617).
Éric Araujo <merwok@netwok.org>
parents: 70795
diff changeset
239
74179a79d673 Improve documentation for PEP 370 support in site module (#8617).
Éric Araujo <merwok@netwok.org>
parents: 70795
diff changeset
240 The Customization Modules
74179a79d673 Improve documentation for PEP 370 support in site module (#8617).
Éric Araujo <merwok@netwok.org>
parents: 70795
diff changeset
241 -------------------------
74179a79d673 Improve documentation for PEP 370 support in site module (#8617).
Éric Araujo <merwok@netwok.org>
parents: 70795
diff changeset
242
74179a79d673 Improve documentation for PEP 370 support in site module (#8617).
Éric Araujo <merwok@netwok.org>
parents: 70795
diff changeset
243 Python provides two hooks to let you customize it: :mod:`sitecustomize` and
74179a79d673 Improve documentation for PEP 370 support in site module (#8617).
Éric Araujo <merwok@netwok.org>
parents: 70795
diff changeset
244 :mod:`usercustomize`. To see how it works, you need first to find the location
74179a79d673 Improve documentation for PEP 370 support in site module (#8617).
Éric Araujo <merwok@netwok.org>
parents: 70795
diff changeset
245 of your user site-packages directory. Start Python and run this code:
74179a79d673 Improve documentation for PEP 370 support in site module (#8617).
Éric Araujo <merwok@netwok.org>
parents: 70795
diff changeset
246
74179a79d673 Improve documentation for PEP 370 support in site module (#8617).
Éric Araujo <merwok@netwok.org>
parents: 70795
diff changeset
247 >>> import site
74179a79d673 Improve documentation for PEP 370 support in site module (#8617).
Éric Araujo <merwok@netwok.org>
parents: 70795
diff changeset
248 >>> site.getusersitepackages()
74179a79d673 Improve documentation for PEP 370 support in site module (#8617).
Éric Araujo <merwok@netwok.org>
parents: 70795
diff changeset
249 '/home/user/.local/lib/python3.2/site-packages'
74179a79d673 Improve documentation for PEP 370 support in site module (#8617).
Éric Araujo <merwok@netwok.org>
parents: 70795
diff changeset
250
74179a79d673 Improve documentation for PEP 370 support in site module (#8617).
Éric Araujo <merwok@netwok.org>
parents: 70795
diff changeset
251 Now you can create a file named :file:`usercustomize.py` in that directory and
74179a79d673 Improve documentation for PEP 370 support in site module (#8617).
Éric Araujo <merwok@netwok.org>
parents: 70795
diff changeset
252 put anything you want in it. It will affect every invocation of Python, unless
74179a79d673 Improve documentation for PEP 370 support in site module (#8617).
Éric Araujo <merwok@netwok.org>
parents: 70795
diff changeset
253 it is started with the :option:`-s` option to disable the automatic import.
74179a79d673 Improve documentation for PEP 370 support in site module (#8617).
Éric Araujo <merwok@netwok.org>
parents: 70795
diff changeset
254
74179a79d673 Improve documentation for PEP 370 support in site module (#8617).
Éric Araujo <merwok@netwok.org>
parents: 70795
diff changeset
255 :mod:`sitecustomize` works in the same way, but is typically created by an
74179a79d673 Improve documentation for PEP 370 support in site module (#8617).
Éric Araujo <merwok@netwok.org>
parents: 70795
diff changeset
256 administrator of the computer in the global site-packages directory, and is
74179a79d673 Improve documentation for PEP 370 support in site module (#8617).
Éric Araujo <merwok@netwok.org>
parents: 70795
diff changeset
257 imported before :mod:`usercustomize`. See the documentation of the :mod:`site`
74179a79d673 Improve documentation for PEP 370 support in site module (#8617).
Éric Araujo <merwok@netwok.org>
parents: 70795
diff changeset
258 module for more details.
74179a79d673 Improve documentation for PEP 370 support in site module (#8617).
Éric Araujo <merwok@netwok.org>
parents: 70795
diff changeset
259
74179a79d673 Improve documentation for PEP 370 support in site module (#8617).
Éric Araujo <merwok@netwok.org>
parents: 70795
diff changeset
260
42604
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
261 .. rubric:: Footnotes
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
262
56062
9817915ebdeb #6935: update version.
Georg Brandl <georg@python.org>
parents: 50514
diff changeset
263 .. [#] 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
264 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
265 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
266
42604
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
267 .. [#] A problem with the GNU Readline package may prevent this.