rest25/tutorial/interpreter.rst => rest262/tutorial/interpreter.rst
17   python
18
19to the shell.  Since the choice of the directory where the interpreter lives is
20an installation option, other places are possible; check with your local Python
21guru or system administrator.  (E.g., :file:`/usr/local/python` is a popular
22alternative location.)
23
24On Windows machines, the Python installation is usually placed in
n25-:file:`C:\Python24`, though you can change this when you're running the
n25+:file:`C:\\Python26`, though you can change this when you're running the
26installer.  To add this directory to your path,  you can type the following
27command into the command prompt in a DOS box::
28
n29-   set path=%path%;C:\python24
n29+   set path=%path%;C:\python26
30
31Typing an end-of-file character (:kbd:`Control-D` on Unix, :kbd:`Control-Z` on
32Windows) at the primary prompt causes the interpreter to exit with a zero exit
33status.  If that doesn't work, you can exit the interpreter by typing the
34following commands: ``import sys; sys.exit()``.
35
36The interpreter's line-editing features usually aren't very sophisticated.  On
37Unix, whoever installed the interpreter may have enabled support for the GNU
46The interpreter operates somewhat like the Unix shell: when called with standard
47input connected to a tty device, it reads and executes commands interactively;
48when called with a file name argument or with a file as standard input, it reads
49and executes a *script* from that file.
50
51A second way of starting the interpreter is ``python -c command [arg] ...``,
52which executes the statement(s) in *command*, analogous to the shell's
53:option:`-c` option.  Since Python statements often contain spaces or other
n54-characters that are special to the shell, it is best to quote  *command* in its
n54+characters that are special to the shell, it is usually advised to quote
55-entirety with double quotes.
55+*command* in its entirety with single quotes.
56
57Some 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
59if you had spelled out its full name on the command line.
60
61Note that there is a difference between ``python file`` and ``python <file``.
62In the latter case, input requests from the program, such as calls to
63:func:`input` and :func:`raw_input`, are satisfied from *file*.  Since this file
97When commands are read from a tty, the interpreter is said to be in *interactive
98mode*.  In this mode it prompts for the next command with the *primary prompt*,
99usually three greater-than signs (``>>>``); for continuation lines it prompts
100with the *secondary prompt*, by default three dots (``...``). The interpreter
101prints a welcome message stating its version number and a copyright notice
102before printing the first prompt::
103
104   python
n105-   Python 1.5.2b2 (#1, Feb 28 1999, 00:02:06)  [GCC 2.8.1] on sunos5
n105+   Python 2.6 (#1, Feb 28 2007, 00:02:06)
106-   Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
106+   Type "help", "copyright", "credits" or "license" for more information.
107   >>>
108
109Continuation lines are needed when entering a multi-line construct. As an
110example, take a look at this :keyword:`if` statement::
111
112   >>> the_world_is_flat = 1
113   >>> if the_world_is_flat:
114   ...     print "Be careful not to fall off!"
n115-   ... 
n115+   ...
116   Be careful not to fall off!
117
118
119.. _tut-interp:
120
121The Interpreter and Its Environment
122===================================
123
152On BSD'ish Unix systems, Python scripts can be made directly executable, like
153shell scripts, by putting the line ::
154
155   #! /usr/bin/env python
156
157(assuming that the interpreter is on the user's :envvar:`PATH`) at the beginning
158of the script and giving the file an executable mode.  The ``#!`` must be the
159first two characters of the file.  On some platforms, this first line must end
n160-with a Unix-style line ending (``'\n'``), not a Mac OS (``'\r'``) or Windows
n160+with a Unix-style line ending (``'\n'``), not a Windows (``'\r\n'``) line
161-(``'\r\n'``) line ending.  Note that the hash, or pound, character, ``'#'``, is
161+ending.  Note that the hash, or pound, character, ``'#'``, is used to start a
162-used to start a comment in Python.
162+comment in Python.
163
164The script can be given an executable mode, or permission, using the
165:program:`chmod` command::
166
167   $ chmod +x myscript.py
168
n169-.. % $ <-- bow to font-lock
n169+On Windows systems, there is no notion of an "executable mode".  The Python
170+installer automatically associates ``.py`` files with ``python.exe`` so that
171+a double-click on a Python file will run it as a script.  The extension can
172+also be ``.pyw``, in that case, the console window that normally appears is
173+suppressed.
170
171
172Source Code Encoding
173--------------------
174
175It is possible to use encodings different than ASCII in Python source files. The
176best way to do it is to put one more special comment line right after the ``#!``
177line to define the source file encoding::
178
n179-   # -\*- coding: encoding -\*- 
n183+   # -*- coding: encoding -*-
180
181
182With that declaration, all characters in the source file will be treated as
183having the encoding *encoding*, and it will be possible to directly write
184Unicode string literals in the selected encoding.  The list of possible
n185-encodings can be found in the Python Library Reference (XXX reference:
n189+encodings can be found in the Python Library Reference, in the section on
186-../lib/lib.html), in the section on :mod:`codecs` (XXX reference: ../lib/module-
190+:mod:`codecs`.
187-codecs.html).
188
189For example, to write Unicode literals including the Euro currency symbol, the
190ISO-8859-15 encoding can be used, with the Euro symbol having the ordinal value
191164.  This script will print the value 8364 (the Unicode codepoint corresponding
192to the Euro symbol) and then exit::
193
194   # -*- coding: iso-8859-15 -*-
195
217----------------------------
218
219When you use Python interactively, it is frequently handy to have some standard
220commands executed every time the interpreter is started.  You can do this by
221setting an environment variable named :envvar:`PYTHONSTARTUP` to the name of a
222file containing your start-up commands.  This is similar to the :file:`.profile`
223feature of the Unix shells.
224
t225-.. % XXX This should probably be dumped in an appendix, since most people
t228+.. XXX This should probably be dumped in an appendix, since most people
226-.. % don't use Python interactively in non-trivial ways.
229+   don't use Python interactively in non-trivial ways.
227
228This file is only read in interactive sessions, not when Python reads commands
229from a script, and not when :file:`/dev/tty` is given as the explicit source of
230commands (which otherwise behaves like an interactive session).  It is executed
231in the same namespace where interactive commands are executed, so that objects
232that it defines or imports can be used without qualification in the interactive
233session. You can also change the prompts ``sys.ps1`` and ``sys.ps2`` in this
234file.
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op