blob: fc9f7332a0b7988ce0ea084d65eaf15b4b8f6422 [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001.. _tut-using:
2
3****************************
4Using the Python Interpreter
5****************************
6
7
8.. _tut-invoking:
9
10Invoking the Interpreter
11========================
12
Georg Brandl08a90122012-09-29 09:34:13 +020013The Python interpreter is usually installed as :file:`/usr/local/bin/python3.4`
Georg Brandl3db38ce2008-08-30 09:58:30 +000014on those machines where it is available; putting :file:`/usr/local/bin` in your
Chris Jerdonekdf12f2b2012-09-25 04:20:29 -070015Unix shell's search path makes it possible to start it by typing the command:
16
17.. code-block:: text
Georg Brandl116aa622007-08-15 14:28:22 +000018
Georg Brandl08a90122012-09-29 09:34:13 +020019 python3.4
Georg Brandl116aa622007-08-15 14:28:22 +000020
Georg Brandl3db38ce2008-08-30 09:58:30 +000021to the shell. [#]_ Since the choice of the directory where the interpreter lives
22is an installation option, other places are possible; check with your local
23Python guru or system administrator. (E.g., :file:`/usr/local/python` is a
24popular alternative location.)
Georg Brandl116aa622007-08-15 14:28:22 +000025
26On Windows machines, the Python installation is usually placed in
Georg Brandl08a90122012-09-29 09:34:13 +020027:file:`C:\\Python34`, though you can change this when you're running the
Georg Brandl116aa622007-08-15 14:28:22 +000028installer. To add this directory to your path, you can type the following
29command into the command prompt in a DOS box::
30
Georg Brandl08a90122012-09-29 09:34:13 +020031 set path=%path%;C:\python34
Georg Brandl116aa622007-08-15 14:28:22 +000032
33Typing an end-of-file character (:kbd:`Control-D` on Unix, :kbd:`Control-Z` on
34Windows) at the primary prompt causes the interpreter to exit with a zero exit
35status. If that doesn't work, you can exit the interpreter by typing the
Benjamin Peterson4ac9ce42009-10-04 14:49:41 +000036following command: ``quit()``.
Georg Brandl116aa622007-08-15 14:28:22 +000037
R David Murray0e0e3912014-04-15 20:25:18 -040038The interpreter's line-editing features include interactive editing, history
R David Murrayfc1020d2014-04-15 20:26:54 -040039substitution and code completion on systems that support readline. Perhaps the
40quickest check to see whether command line editing is supported is typing
41Control-P to the first Python prompt you get. If it beeps, you have command
42line editing; see Appendix :ref:`tut-interacting` for an introduction to the
43keys. If nothing appears to happen, or if ``^P`` is echoed, command line
44editing isn't available; you'll only be able to use backspace to remove
45characters from the current line.
Georg Brandl116aa622007-08-15 14:28:22 +000046
47The interpreter operates somewhat like the Unix shell: when called with standard
48input connected to a tty device, it reads and executes commands interactively;
49when called with a file name argument or with a file as standard input, it reads
50and executes a *script* from that file.
51
52A second way of starting the interpreter is ``python -c command [arg] ...``,
53which executes the statement(s) in *command*, analogous to the shell's
54:option:`-c` option. Since Python statements often contain spaces or other
Georg Brandlf08a9dd2008-06-10 16:57:31 +000055characters that are special to the shell, it is usually advised to quote
56*command* in its entirety with single quotes.
Georg Brandl116aa622007-08-15 14:28:22 +000057
58Some Python modules are also useful as scripts. These can be invoked using
59``python -m module [arg] ...``, which executes the source file for *module* as
60if you had spelled out its full name on the command line.
61
Georg Brandl116aa622007-08-15 14:28:22 +000062When a script file is used, it is sometimes useful to be able to run the script
63and enter interactive mode afterwards. This can be done by passing :option:`-i`
Sandro Tosi69e59a12011-10-31 17:15:39 +010064before the script.
Georg Brandl116aa622007-08-15 14:28:22 +000065
66
67.. _tut-argpassing:
68
69Argument Passing
70----------------
71
72When known to the interpreter, the script name and additional arguments
R. David Murraya3964632010-12-17 16:11:40 +000073thereafter are turned into a list of strings and assigned to the ``argv``
74variable in the ``sys`` module. You can access this list by executing ``import
75sys``. The length of the list is at least one; when no script and no arguments
Georg Brandl116aa622007-08-15 14:28:22 +000076are given, ``sys.argv[0]`` is an empty string. When the script name is given as
77``'-'`` (meaning standard input), ``sys.argv[0]`` is set to ``'-'``. When
78:option:`-c` *command* is used, ``sys.argv[0]`` is set to ``'-c'``. When
79:option:`-m` *module* is used, ``sys.argv[0]`` is set to the full name of the
80located module. Options found after :option:`-c` *command* or :option:`-m`
81*module* are not consumed by the Python interpreter's option processing but
82left in ``sys.argv`` for the command or module to handle.
83
84
85.. _tut-interactive:
86
87Interactive Mode
88----------------
89
90When commands are read from a tty, the interpreter is said to be in *interactive
91mode*. In this mode it prompts for the next command with the *primary prompt*,
92usually three greater-than signs (``>>>``); for continuation lines it prompts
93with the *secondary prompt*, by default three dots (``...``). The interpreter
94prints a welcome message stating its version number and a copyright notice
95before printing the first prompt::
96
Georg Brandl08a90122012-09-29 09:34:13 +020097 $ python3.4
Georg Brandl75c5ab42014-03-22 20:38:11 +010098 Python 3.4 (default, Mar 16 2014, 09:25:04)
Georg Brandl553e1082014-03-23 23:03:59 +010099 [GCC 4.8.2] on linux
Georg Brandl2d2590d2007-09-28 13:13:35 +0000100 Type "help", "copyright", "credits" or "license" for more information.
Georg Brandl116aa622007-08-15 14:28:22 +0000101 >>>
102
Georg Brandla17487b2009-09-18 07:27:51 +0000103.. XXX update for new releases
Georg Brandl2d2590d2007-09-28 13:13:35 +0000104
Georg Brandl116aa622007-08-15 14:28:22 +0000105Continuation lines are needed when entering a multi-line construct. As an
106example, take a look at this :keyword:`if` statement::
107
Raymond Hettinger4ab532b2014-03-28 16:39:25 -0700108 >>> the_world_is_flat = True
Georg Brandl116aa622007-08-15 14:28:22 +0000109 >>> if the_world_is_flat:
Georg Brandl6911e3c2007-09-04 07:15:32 +0000110 ... print("Be careful not to fall off!")
Georg Brandl48310cd2009-01-03 21:18:54 +0000111 ...
Georg Brandl116aa622007-08-15 14:28:22 +0000112 Be careful not to fall off!
113
114
Senthil Kumaran15e48332014-09-18 21:30:28 +0800115For more on interactive mode, see :ref:`tut-interac`.
116
117
Georg Brandl116aa622007-08-15 14:28:22 +0000118.. _tut-interp:
119
120The Interpreter and Its Environment
121===================================
122
123
Éric Araujo9fbfe152011-06-11 10:34:19 +0200124.. _tut-source-encoding:
125
Georg Brandl116aa622007-08-15 14:28:22 +0000126Source Code Encoding
127--------------------
128
Georg Brandl2d2590d2007-09-28 13:13:35 +0000129By default, Python source files are treated as encoded in UTF-8. In that
130encoding, characters of most languages in the world can be used simultaneously
131in string literals, identifiers and comments --- although the standard library
132only uses ASCII characters for identifiers, a convention that any portable code
133should follow. To display all these characters properly, your editor must
134recognize that the file is UTF-8, and it must use a font that supports all the
135characters in the file.
Georg Brandl6911e3c2007-09-04 07:15:32 +0000136
Georg Brandl2d2590d2007-09-28 13:13:35 +0000137It is also possible to specify a different encoding for source files. In order
138to do this, put one more special comment line right after the ``#!`` line to
139define the source file encoding::
Georg Brandl116aa622007-08-15 14:28:22 +0000140
Georg Brandl48310cd2009-01-03 21:18:54 +0000141 # -*- coding: encoding -*-
Georg Brandl116aa622007-08-15 14:28:22 +0000142
Georg Brandl2d2590d2007-09-28 13:13:35 +0000143With that declaration, everything in the source file will be treated as having
144the encoding *encoding* instead of UTF-8. The list of possible encodings can be
145found in the Python Library Reference, in the section on :mod:`codecs`.
Georg Brandl116aa622007-08-15 14:28:22 +0000146
Georg Brandl2d2590d2007-09-28 13:13:35 +0000147For example, if your editor of choice does not support UTF-8 encoded files and
148insists on using some other encoding, say Windows-1252, you can write::
Georg Brandl116aa622007-08-15 14:28:22 +0000149
Georg Brandl2d2590d2007-09-28 13:13:35 +0000150 # -*- coding: cp-1252 -*-
Georg Brandl116aa622007-08-15 14:28:22 +0000151
Georg Brandl2d2590d2007-09-28 13:13:35 +0000152and still use all characters in the Windows-1252 character set in the source
153files. The special encoding comment must be in the *first or second* line
154within the file.
Georg Brandl116aa622007-08-15 14:28:22 +0000155
156
Georg Brandl116aa622007-08-15 14:28:22 +0000157.. rubric:: Footnotes
158
Georg Brandla17487b2009-09-18 07:27:51 +0000159.. [#] On Unix, the Python 3.x interpreter is by default not installed with the
Georg Brandl3db38ce2008-08-30 09:58:30 +0000160 executable named ``python``, so that it does not conflict with a
161 simultaneously installed Python 2.x executable.