blob: 3bf9c62ca87cc1d77940160e73c7bc72814d04a1 [file] [log] [blame]
Georg Brandl8ec7f652007-08-15 14:28:01 +00001.. _tut-using:
2
3****************************
4Using the Python Interpreter
5****************************
6
7
8.. _tut-invoking:
9
10Invoking the Interpreter
11========================
12
13The Python interpreter is usually installed as :file:`/usr/local/bin/python` on
14those machines where it is available; putting :file:`/usr/local/bin` in your
15Unix shell's search path makes it possible to start it by typing the command ::
16
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
Georg Brandl9352f1c2010-04-10 11:16:59 +000025:file:`C:\\Python27`, though you can change this when you're running the
Georg Brandl8ec7f652007-08-15 14:28:01 +000026installer. To add this directory to your path, you can type the following
27command into the command prompt in a DOS box::
28
Georg Brandl9352f1c2010-04-10 11:16:59 +000029 set path=%path%;C:\python27
Georg Brandl8ec7f652007-08-15 14:28:01 +000030
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
Georg Brandl4d94d312009-09-18 07:22:41 +000034following command: ``quit()``.
Georg Brandl8ec7f652007-08-15 14:28:01 +000035
36The interpreter's line-editing features usually aren't very sophisticated. On
37Unix, whoever installed the interpreter may have enabled support for the GNU
38readline library, which adds more elaborate interactive editing and history
39features. Perhaps the quickest check to see whether command line editing is
Serhiy Storchaka9b2e37f2015-09-12 17:47:12 +030040supported is typing :kbd:`Control-P` to the first Python prompt you get. If it beeps,
Georg Brandl8ec7f652007-08-15 14:28:01 +000041you have command line editing; see Appendix :ref:`tut-interacting` for an
42introduction to the keys. If nothing appears to happen, or if ``^P`` is echoed,
43command line editing isn't available; you'll only be able to use backspace to
44remove characters from the current line.
45
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
Georg Brandlc5a235b2008-05-30 19:17:29 +000054characters that are special to the shell, it is usually advised to quote
55*command* in its entirety with single quotes.
Georg Brandl8ec7f652007-08-15 14:28:01 +000056
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
Georg Brandl8ec7f652007-08-15 14:28:01 +000061When a script file is used, it is sometimes useful to be able to run the script
62and enter interactive mode afterwards. This can be done by passing :option:`-i`
Sandro Tosic93e4132011-10-31 17:15:03 +010063before the script.
Georg Brandl8ec7f652007-08-15 14:28:01 +000064
Berker Peksag3e1c8232014-12-10 02:07:08 +020065All command-line options are described in :ref:`using-on-general`.
66
Georg Brandl8ec7f652007-08-15 14:28:01 +000067
68.. _tut-argpassing:
69
70Argument Passing
71----------------
72
73When known to the interpreter, the script name and additional arguments
R. David Murray561b96f2011-02-11 17:25:54 +000074thereafter are turned into a list of strings and assigned to the ``argv``
75variable in the ``sys`` module. You can access this list by executing ``import
76sys``. The length of the list is at least one; when no script and no arguments
Georg Brandl8ec7f652007-08-15 14:28:01 +000077are given, ``sys.argv[0]`` is an empty string. When the script name is given as
78``'-'`` (meaning standard input), ``sys.argv[0]`` is set to ``'-'``. When
79:option:`-c` *command* is used, ``sys.argv[0]`` is set to ``'-c'``. When
80:option:`-m` *module* is used, ``sys.argv[0]`` is set to the full name of the
81located module. Options found after :option:`-c` *command* or :option:`-m`
82*module* are not consumed by the Python interpreter's option processing but
83left in ``sys.argv`` for the command or module to handle.
84
85
86.. _tut-interactive:
87
88Interactive Mode
89----------------
90
91When commands are read from a tty, the interpreter is said to be in *interactive
92mode*. In this mode it prompts for the next command with the *primary prompt*,
93usually three greater-than signs (``>>>``); for continuation lines it prompts
94with the *secondary prompt*, by default three dots (``...``). The interpreter
95prints a welcome message stating its version number and a copyright notice
Martin Panter8f1dd222016-07-26 11:18:21 +020096before printing the first prompt:
97
98.. code-block:: shell-session
Georg Brandl8ec7f652007-08-15 14:28:01 +000099
100 python
Georg Brandl9352f1c2010-04-10 11:16:59 +0000101 Python 2.7 (#1, Feb 28 2010, 00:02:06)
Neal Norwitz76e4d622007-11-19 01:46:20 +0000102 Type "help", "copyright", "credits" or "license" for more information.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000103 >>>
104
105Continuation lines are needed when entering a multi-line construct. As an
106example, take a look at this :keyword:`if` statement::
107
108 >>> the_world_is_flat = 1
109 >>> if the_world_is_flat:
110 ... print "Be careful not to fall off!"
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000111 ...
Georg Brandl8ec7f652007-08-15 14:28:01 +0000112 Be careful not to fall off!
113
114
Senthil Kumaran74012c12014-09-18 21:29:21 +0800115For more on interactive mode, see :ref:`tut-interac`.
116
117
Georg Brandl8ec7f652007-08-15 14:28:01 +0000118.. _tut-interp:
119
120The Interpreter and Its Environment
121===================================
122
123
Éric Araujoec464cf2011-07-29 11:35:27 +0200124.. _tut-source-encoding:
125
Georg Brandl8ec7f652007-08-15 14:28:01 +0000126Source Code Encoding
127--------------------
128
Mariatta Wijaya40ba60f2017-02-01 21:14:47 -0800129By 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.
136
137To declare an encoding other than the default one, a special comment line
138should be added as the *first* line of the file. The syntax is as follows::
Georg Brandl8ec7f652007-08-15 14:28:01 +0000139
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000140 # -*- coding: encoding -*-
Georg Brandl8ec7f652007-08-15 14:28:01 +0000141
Mariatta Wijaya40ba60f2017-02-01 21:14:47 -0800142where *encoding* is one of the valid :mod:`codecs` supported by Python.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000143
Mariatta Wijaya40ba60f2017-02-01 21:14:47 -0800144For example, to declare that Windows-1252 encoding is to be used, the first
145line of your source code file should be::
Georg Brandl8ec7f652007-08-15 14:28:01 +0000146
Mariatta Wijaya40ba60f2017-02-01 21:14:47 -0800147 # -*- coding: cp-1252 -*-
Georg Brandl8ec7f652007-08-15 14:28:01 +0000148
Mariatta Wijaya40ba60f2017-02-01 21:14:47 -0800149One exception to the *first line* rule is when the source code starts with a
150:ref:`UNIX "shebang" line <tut-scripts>`. In this case, the encoding
151declaration should be added as the second line of the file. For example::
Georg Brandl8ec7f652007-08-15 14:28:01 +0000152
Mariatta Wijaya40ba60f2017-02-01 21:14:47 -0800153 #!/usr/bin/env python
154 # -*- coding: cp-1252 -*-
Georg Brandl8ec7f652007-08-15 14:28:01 +0000155