blob: b78d2960ac5432a2b6e6c353309c591d1c879eff [file] [log] [blame]
Berker Peksagc5f40362016-06-01 14:38:18 -07001.. _tut-using:
Georg Brandl116aa622007-08-15 14:28:22 +00002
3****************************
4Using the Python Interpreter
5****************************
6
7
8.. _tut-invoking:
9
10Invoking the Interpreter
11========================
12
Łukasz Langa9ab2fb12019-06-04 22:12:32 +020013The Python interpreter is usually installed as :file:`/usr/local/bin/python3.9`
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
Łukasz Langa9ab2fb12019-06-04 22:12:32 +020019 python3.9
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
Jules Lasne (jlasne)5e01a652019-11-05 14:20:38 +010026On Windows machines where you have installed Python from the :ref:`Microsoft Store
Steve Dower7a177c02019-06-26 08:55:57 -070027<windows-store>`, the :file:`python3.9` command will be available. If you have
28the :ref:`py.exe launcher <launcher>` installed, you can use the :file:`py`
29command. See :ref:`setting-envvars` for other ways to launch Python.
Georg Brandl116aa622007-08-15 14:28:22 +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
Benjamin Peterson4ac9ce42009-10-04 14:49:41 +000034following command: ``quit()``.
Georg Brandl116aa622007-08-15 14:28:22 +000035
R David Murray0e0e3912014-04-15 20:25:18 -040036The interpreter's line-editing features include interactive editing, history
Adorilson Bezerraf18242b2019-09-16 13:18:04 -030037substitution and code completion on systems that support the `GNU Readline
38<https://tiswww.case.edu/php/chet/readline/rltop.html>`_ library.
39Perhaps the quickest check to see whether command line editing is supported is
40typing :kbd:`Control-P` to the first Python prompt you get. If it beeps, you
41have command line editing; see Appendix :ref:`tut-interacting` for an
42introduction to the keys. If nothing appears to happen, or if ``^P`` is
43echoed, command line editing isn't available; you'll only be able to use
44backspace to remove characters from the current line.
Georg Brandl116aa622007-08-15 14:28:22 +000045
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 Brandlf08a9dd2008-06-10 16:57:31 +000054characters that are special to the shell, it is usually advised to quote
55*command* in its entirety with single quotes.
Georg Brandl116aa622007-08-15 14:28:22 +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 Brandl116aa622007-08-15 14:28:22 +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 Tosi69e59a12011-10-31 17:15:39 +010063before the script.
Georg Brandl116aa622007-08-15 14:28:22 +000064
Berker Peksag8b1cbd22014-12-10 01:47:02 +020065All command line options are described in :ref:`using-on-general`.
66
Georg Brandl116aa622007-08-15 14:28:22 +000067
68.. _tut-argpassing:
69
70Argument Passing
71----------------
72
73When known to the interpreter, the script name and additional arguments
R. David Murraya3964632010-12-17 16:11:40 +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 Brandl116aa622007-08-15 14:28:22 +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 Panter1050d2d2016-07-26 11:18:21 +020096before printing the first prompt:
97
98.. code-block:: shell-session
Georg Brandl116aa622007-08-15 14:28:22 +000099
Łukasz Langa9ab2fb12019-06-04 22:12:32 +0200100 $ python3.9
101 Python 3.9 (default, June 4 2019, 09:25:04)
Georg Brandl553e1082014-03-23 23:03:59 +0100102 [GCC 4.8.2] on linux
Georg Brandl2d2590d2007-09-28 13:13:35 +0000103 Type "help", "copyright", "credits" or "license" for more information.
Georg Brandl116aa622007-08-15 14:28:22 +0000104 >>>
105
Georg Brandla17487b2009-09-18 07:27:51 +0000106.. XXX update for new releases
Georg Brandl2d2590d2007-09-28 13:13:35 +0000107
Georg Brandl116aa622007-08-15 14:28:22 +0000108Continuation lines are needed when entering a multi-line construct. As an
109example, take a look at this :keyword:`if` statement::
110
Raymond Hettinger4ab532b2014-03-28 16:39:25 -0700111 >>> the_world_is_flat = True
Georg Brandl116aa622007-08-15 14:28:22 +0000112 >>> if the_world_is_flat:
Georg Brandl6911e3c2007-09-04 07:15:32 +0000113 ... print("Be careful not to fall off!")
Georg Brandl48310cd2009-01-03 21:18:54 +0000114 ...
Georg Brandl116aa622007-08-15 14:28:22 +0000115 Be careful not to fall off!
116
117
Senthil Kumaran15e48332014-09-18 21:30:28 +0800118For more on interactive mode, see :ref:`tut-interac`.
119
120
Georg Brandl116aa622007-08-15 14:28:22 +0000121.. _tut-interp:
122
123The Interpreter and Its Environment
124===================================
125
126
Éric Araujo9fbfe152011-06-11 10:34:19 +0200127.. _tut-source-encoding:
128
Georg Brandl116aa622007-08-15 14:28:22 +0000129Source Code Encoding
130--------------------
131
Georg Brandl2d2590d2007-09-28 13:13:35 +0000132By default, Python source files are treated as encoded in UTF-8. In that
133encoding, characters of most languages in the world can be used simultaneously
134in string literals, identifiers and comments --- although the standard library
135only uses ASCII characters for identifiers, a convention that any portable code
136should follow. To display all these characters properly, your editor must
137recognize that the file is UTF-8, and it must use a font that supports all the
138characters in the file.
Georg Brandl6911e3c2007-09-04 07:15:32 +0000139
Mariatta Wijaya23dcccb2017-02-01 20:55:47 -0800140To declare an encoding other than the default one, a special comment line
141should be added as the *first* line of the file. The syntax is as follows::
Georg Brandl116aa622007-08-15 14:28:22 +0000142
Georg Brandl48310cd2009-01-03 21:18:54 +0000143 # -*- coding: encoding -*-
Georg Brandl116aa622007-08-15 14:28:22 +0000144
Mariatta Wijaya23dcccb2017-02-01 20:55:47 -0800145where *encoding* is one of the valid :mod:`codecs` supported by Python.
Georg Brandl116aa622007-08-15 14:28:22 +0000146
Mariatta Wijaya23dcccb2017-02-01 20:55:47 -0800147For example, to declare that Windows-1252 encoding is to be used, the first
148line of your source code file should be::
Georg Brandl116aa622007-08-15 14:28:22 +0000149
Serhiy Storchakaddb62152018-05-09 11:10:55 +0300150 # -*- coding: cp1252 -*-
Georg Brandl116aa622007-08-15 14:28:22 +0000151
Mariatta Wijaya23dcccb2017-02-01 20:55:47 -0800152One exception to the *first line* rule is when the source code starts with a
153:ref:`UNIX "shebang" line <tut-scripts>`. In this case, the encoding
154declaration should be added as the second line of the file. For example::
Georg Brandl116aa622007-08-15 14:28:22 +0000155
Mariatta Wijaya23dcccb2017-02-01 20:55:47 -0800156 #!/usr/bin/env python3
Serhiy Storchakaddb62152018-05-09 11:10:55 +0300157 # -*- coding: cp1252 -*-
Georg Brandl116aa622007-08-15 14:28:22 +0000158
Georg Brandl116aa622007-08-15 14:28:22 +0000159.. rubric:: Footnotes
160
Georg Brandla17487b2009-09-18 07:27:51 +0000161.. [#] On Unix, the Python 3.x interpreter is by default not installed with the
Georg Brandl3db38ce2008-08-30 09:58:30 +0000162 executable named ``python``, so that it does not conflict with a
163 simultaneously installed Python 2.x executable.