Debian Codegraph @

Doc/tutorial/appendix.rst

raw
1 .. _tut-appendix:
2
3 ********
4 Appendix
5 ********
6
7
8 .. _tut-interac:
9
10 Interactive Mode
11 ================
12
13 There are two variants of the interactive :term:`REPL`. The classic
14 basic interpreter is supported on all platforms with minimal line
15 control capabilities.
16
17 On Windows, or Unix-like systems with :mod:`curses` support,
18 a new interactive shell is used by default since Python 3.13.
19 This one supports color, multiline editing, history browsing, and
20 paste mode. To disable color, see :ref:`using-on-controlling-color` for
21 details. Function keys provide some additional functionality.
22 :kbd:`F1` enters the interactive help browser :mod:`pydoc`.
23 :kbd:`F2` allows for browsing command-line history with neither output nor the
24 :term:`>>>` and :term:`...` prompts. :kbd:`F3` enters "paste mode", which
25 makes pasting larger blocks of code easier. Press :kbd:`F3` to return to
26 the regular prompt.
27
28 When using the new interactive shell, exit the shell by typing :kbd:`exit`
29 or :kbd:`quit`. Adding call parentheses after those commands is not
30 required.
31
32 If the new interactive shell is not desired, it can be disabled via
33 the :envvar:`PYTHON_BASIC_REPL` environment variable.
34
35 .. _tut-error:
36
37 Error Handling
38 --------------
39
40 When an error occurs, the interpreter prints an error message and a stack trace.
41 In interactive mode, it then returns to the primary prompt; when input came from
42 a file, it exits with a nonzero exit status after printing the stack trace.
43 (Exceptions handled by an :keyword:`except` clause in a :keyword:`try` statement
44 are not errors in this context.) Some errors are unconditionally fatal and
45 cause an exit with a nonzero exit status; this applies to internal inconsistencies and
46 some cases of running out of memory. All error messages are written to the
47 standard error stream; normal output from executed commands is written to
48 standard output.
49
50 Typing the interrupt character (usually :kbd:`Control-C` or :kbd:`Delete`) to the primary or
51 secondary prompt cancels the input and returns to the primary prompt. [#]_
52 Typing an interrupt while a command is executing raises the
53 :exc:`KeyboardInterrupt` exception, which may be handled by a :keyword:`try`
54 statement.
55
56
57 .. _tut-scripts:
58
59 Executable Python Scripts
60 -------------------------
61
62 On BSD'ish Unix systems, Python scripts can be made directly executable, like
63 shell scripts, by putting the line ::
64
65 #!/usr/bin/env python3
66
67 (assuming that the interpreter is on the user's :envvar:`PATH`) at the beginning
68 of the script and giving the file an executable mode. The ``#!`` must be the
69 first two characters of the file. On some platforms, this first line must end
70 with a Unix-style line ending (``'\n'``), not a Windows (``'\r\n'``) line
71 ending. Note that the hash, or pound, character, ``'#'``, is used to start a
72 comment in Python.
73
74 The script can be given an executable mode, or permission, using the
75 :program:`chmod` command.
76
77 .. code-block:: shell-session
78
79 $ chmod +x myscript.py
80
81 On Windows systems, there is no notion of an "executable mode". The Python
82 installer automatically associates ``.py`` files with ``python.exe`` so that
83 a double-click on a Python file will run it as a script. The extension can
84 also be ``.pyw``, in that case, the console window that normally appears is
85 suppressed.
86
87
88 .. _tut-startup:
89
90 The Interactive Startup File
91 ----------------------------
92
93 When you use Python interactively, it is frequently handy to have some standard
94 commands executed every time the interpreter is started. You can do this by
95 setting an environment variable named :envvar:`PYTHONSTARTUP` to the name of a
96 file containing your start-up commands. This is similar to the :file:`.profile`
97 feature of the Unix shells.
98
99 This file is only read in interactive sessions, not when Python reads commands
100 from a script, and not when :file:`/dev/tty` is given as the explicit source of
101 commands (which otherwise behaves like an interactive session). It is executed
102 in the same namespace where interactive commands are executed, so that objects
103 that it defines or imports can be used without qualification in the interactive
104 session. You can also change the prompts ``sys.ps1`` and ``sys.ps2`` in this
105 file.
106
107 If you want to read an additional start-up file from the current directory, you
108 can program this in the global start-up file using code like ``if
109 os.path.isfile('.pythonrc.py'): exec(open('.pythonrc.py').read())``.
110 If you want to use the startup file in a script, you must do this explicitly
111 in the script::
112
113 import os
114 filename = os.environ.get('PYTHONSTARTUP')
115 if filename and os.path.isfile(filename):
116 with open(filename) as fobj:
117 startup_file = fobj.read()
118 exec(startup_file)
119
120
121 .. _tut-customize:
122
123 The Customization Modules
124 -------------------------
125
126 Python provides two hooks to let you customize it: :index:`sitecustomize` and
127 :index:`usercustomize`. To see how it works, you need first to find the location
128 of your user site-packages directory. Start Python and run this code::
129
130 >>> import site
131 >>> site.getusersitepackages()
132 '/home/user/.local/lib/python3.x/site-packages'
133
134 Now you can create a file named :file:`usercustomize.py` in that directory and
135 put anything you want in it. It will affect every invocation of Python, unless
136 it is started with the :option:`-s` option to disable the automatic import.
137
138 :index:`sitecustomize` works in the same way, but is typically created by an
139 administrator of the computer in the global site-packages directory, and is
140 imported before :index:`usercustomize`. See the documentation of the :mod:`site`
141 module for more details.
142
143
144 .. rubric:: Footnotes
145
146 .. [#] A problem with the GNU Readline package may prevent this.