-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathappendix.html
394 lines (394 loc) · 13.4 KB
/
appendix.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="" xml:lang="">
<head>
<meta charset="utf-8" />
<meta name="generator" content="pandoc" />
<meta
name="viewport"
content="width=device-width, initial-scale=1.0, user-scalable=yes"
/>
<title>appendix</title>
<style type="text/css">
code {
white-space: pre-wrap;
}
span.smallcaps {
font-variant: small-caps;
}
span.underline {
text-decoration: underline;
}
div.column {
display: inline-block;
vertical-align: top;
width: 50%;
}
</style>
</head>
<body>
<h3 id="navigation">Navigation</h3>
<ul>
<li>
<a href="https://docs.python.org/3/genindex.html" title="General Index"
>index</a
>
</li>
<li>
<a
href="https://docs.python.org/3/py-modindex.html"
title="Python Module Index"
>modules</a
>
|
</li>
<li>
<a
href="https://docs.python.org/3/using/index.html"
title="Python Setup and Usage"
>next</a
>
|
</li>
<li>
<a
href="floatingpoint.html"
title="15. Floating Point Arithmetic: Issues and Limitations"
>previous</a
>
|
</li>
<li><img src="../_static/py.png" /></li>
<li><a href="https://www.python.org/">Python</a> »</li>
<li>
<a href="https://docs.python.org/3/index.html">3.9.5 Documentation</a> »
</li>
<li><a href="index.html">The Python Tutorial</a> »</li>
<li></li>
</ul>
<p><span id="tut-appendix"></span></p>
<h1 id="appendix">
<span class="section-number">16. </span>Appendix<a
href="#appendix"
class="headerlink"
title="Permalink to this headline"
>¶</a
>
</h1>
<p><span id="tut-interac"></span></p>
<h2 id="interactive-mode">
<span class="section-number">16.1. </span>Interactive Mode<a
href="#interactive-mode"
class="headerlink"
title="Permalink to this headline"
>¶</a
>
</h2>
<p><span id="tut-error"></span></p>
<h3 id="error-handling">
<span class="section-number">16.1.1. </span>Error Handling<a
href="#error-handling"
class="headerlink"
title="Permalink to this headline"
>¶</a
>
</h3>
<p>
When an error occurs, the interpreter prints an error message and a stack
trace. In interactive mode, it then returns to the primary prompt; when
input came from a file, it exits with a nonzero exit status after printing
the stack trace. (Exceptions handled by an
<a
href="https://docs.python.org/3/reference/compound_stmts.html#except"
class="reference internal"
><code class="xref std std-keyword docutils literal notranslate"
>except</code
></a
>
clause in a
<a
href="https://docs.python.org/3/reference/compound_stmts.html#try"
class="reference internal"
><code class="xref std std-keyword docutils literal notranslate"
>try</code
></a
>
statement are not errors in this context.) Some errors are unconditionally
fatal and cause an exit with a nonzero exit; this applies to internal
inconsistencies and some cases of running out of memory. All error
messages are written to the standard error stream; normal output from
executed commands is written to standard output.
</p>
<p>
Typing the interrupt character (usually Control-C or Delete) to the
primary or secondary prompt cancels the input and returns to the primary
prompt.
<a href="#id2" id="id1" class="footnote-reference brackets">1</a> Typing
an interrupt while a command is executing raises the
<a
href="https://docs.python.org/3/library/exceptions.html#KeyboardInterrupt"
class="reference internal"
title="KeyboardInterrupt"
><code class="sourceCode python"
><span class="pp">KeyboardInterrupt</span></code
></a
>
exception, which may be handled by a
<a
href="https://docs.python.org/3/reference/compound_stmts.html#try"
class="reference internal"
><code class="xref std std-keyword docutils literal notranslate"
>try</code
></a
>
statement.
</p>
<p><span id="tut-scripts"></span></p>
<h3 id="executable-python-scripts">
<span class="section-number">16.1.2. </span>Executable Python Scripts<a
href="#executable-python-scripts"
class="headerlink"
title="Permalink to this headline"
>¶</a
>
</h3>
<p>
On BSD’ish Unix systems, Python scripts can be made directly executable,
like shell scripts, by putting the line
</p>
<pre><code>#!/usr/bin/env python3.5</code></pre>
<p>
(assuming that the interpreter is on the user’s
<span id="index-0" class="target"></span><code>PATH</code>) at the
beginning of the script and giving the file an executable mode. The
<code>#!</code> must be the first two characters of the file. On some
platforms, this first line must end with a Unix-style line ending
(<code>'\n'</code>), not a Windows (<code>'\r\n'</code>) line ending. Note
that the hash, or pound, character, <code>'#'</code>, is used to start a
comment in Python.
</p>
<p>
The script can be given an executable mode, or permission, using the
<strong>chmod</strong> command.
</p>
<pre><code>$ chmod +x myscript.py</code></pre>
<p>
On Windows systems, there is no notion of an “executable mode”. The Python
installer automatically associates <code>.py</code> files with
<code>python.exe</code> so that a double-click on a Python file will run
it as a script. The extension can also be <code>.pyw</code>, in that case,
the console window that normally appears is suppressed.
</p>
<p><span id="tut-startup"></span></p>
<h3 id="the-interactive-startup-file">
<span class="section-number">16.1.3. </span>The Interactive Startup File<a
href="#the-interactive-startup-file"
class="headerlink"
title="Permalink to this headline"
>¶</a
>
</h3>
<p>
When you use Python interactively, it is frequently handy to have some
standard commands executed every time the interpreter is started. You can
do this by setting an environment variable named
<span id="index-1" class="target"></span
><a
href="https://docs.python.org/3/using/cmdline.html#envvar-PYTHONSTARTUP"
class="reference internal"
><code class="xref std std-envvar docutils literal notranslate"
>PYTHONSTARTUP</code
></a
>
to the name of a file containing your start-up commands. This is similar
to the <code>.profile</code> feature of the Unix shells.
</p>
<p>
This file is only read in interactive sessions, not when Python reads
commands from a script, and not when <code>/dev/tty</code> is given as the
explicit source of commands (which otherwise behaves like an interactive
session). It is executed in the same namespace where interactive commands
are executed, so that objects that it defines or imports can be used
without qualification in the interactive session. You can also change the
prompts <code>sys.ps1</code> and <code>sys.ps2</code> in this file.
</p>
<p>
If you want to read an additional start-up file from the current
directory, you can program this in the global start-up file using code
like
<code
>if os.path.isfile('.pythonrc.py'):
exec(open('.pythonrc.py').read())</code
>. If you want to use the startup file in a script, you must do this
explicitly in the script:
</p>
<pre><code>import os
filename = os.environ.get('PYTHONSTARTUP')
if filename and os.path.isfile(filename):
with open(filename) as fobj:
startup_file = fobj.read()
exec(startup_file)</code></pre>
<p><span id="tut-customize"></span></p>
<h3 id="the-customization-modules">
<span class="section-number">16.1.4. </span>The Customization Modules<a
href="#the-customization-modules"
class="headerlink"
title="Permalink to this headline"
>¶</a
>
</h3>
<p>
Python provides two hooks to let you customize it:
<code>sitecustomize</code> and <code>usercustomize</code>. To see how it
works, you need first to find the location of your user site-packages
directory. Start Python and run this code:
</p>
<pre><code>>>> import site
>>> site.getusersitepackages()
'/home/user/.local/lib/python3.5/site-packages'</code></pre>
<p>
Now you can create a file named <code>usercustomize.py</code> in that
directory and put anything you want in it. It will affect every invocation
of Python, unless it is started with the
<a
href="https://docs.python.org/3/using/cmdline.html#cmdoption-s"
class="reference internal"
><code class="xref std std-option docutils literal notranslate"
>-s</code
></a
>
option to disable the automatic import.
</p>
<p>
<code>sitecustomize</code> works in the same way, but is typically created
by an administrator of the computer in the global site-packages directory,
and is imported before <code>usercustomize</code>. See the documentation
of the
<a
href="https://docs.python.org/3/library/site.html#module-site"
class="reference internal"
title="site: Module responsible for site-specific configuration."
><code class="sourceCode python">site</code></a
>
module for more details.
</p>
<p>Footnotes</p>
<p>
<span class="brackets"><a href="#id1" class="fn-backref">1</a></span
><br />
A problem with the GNU Readline package may prevent this.
</p>
<h3 id="table-of-contents">
<a href="https://docs.python.org/3/contents.html">Table of Contents</a>
</h3>
<ul>
<li>
<a href="#" class="reference internal">16. Appendix</a>
<ul>
<li>
<a href="#interactive-mode" class="reference internal"
>16.1. Interactive Mode</a
>
<ul>
<li>
<a href="#error-handling" class="reference internal"
>16.1.1. Error Handling</a
>
</li>
<li>
<a href="#executable-python-scripts" class="reference internal"
>16.1.2. Executable Python Scripts</a
>
</li>
<li>
<a
href="#the-interactive-startup-file"
class="reference internal"
>16.1.3. The Interactive Startup File</a
>
</li>
<li>
<a href="#the-customization-modules" class="reference internal"
>16.1.4. The Customization Modules</a
>
</li>
</ul>
</li>
</ul>
</li>
</ul>
<h4 id="previous-topic">Previous topic</h4>
<p>
<a href="floatingpoint.html" title="previous chapter"
><span class="section-number">15. </span>Floating Point Arithmetic:
Issues and Limitations</a
>
</p>
<h4 id="next-topic">Next topic</h4>
<p>
<a href="https://docs.python.org/3/using/index.html" title="next chapter"
>Python Setup and Usage</a
>
</p>
<h3 id="this-page">This Page</h3>
<ul>
<li><a href="https://docs.python.org/3/bugs.html">Report a Bug</a></li>
<li>
<a
href="https://github.com/python/cpython/blob/3.9/Doc/tutorial/appendix.rst"
>Show Source</a
>
</li>
</ul>
<h3 id="navigation-1">Navigation</h3>
<ul>
<li>
<a href="https://docs.python.org/3/genindex.html" title="General Index"
>index</a
>
</li>
<li>
<a
href="https://docs.python.org/3/py-modindex.html"
title="Python Module Index"
>modules</a
>
|
</li>
<li>
<a
href="https://docs.python.org/3/using/index.html"
title="Python Setup and Usage"
>next</a
>
|
</li>
<li>
<a
href="floatingpoint.html"
title="15. Floating Point Arithmetic: Issues and Limitations"
>previous</a
>
|
</li>
<li><img src="../_static/py.png" /></li>
<li><a href="https://www.python.org/">Python</a> »</li>
<li>
<a href="https://docs.python.org/3/index.html">3.9.5 Documentation</a> »
</li>
<li><a href="index.html">The Python Tutorial</a> »</li>
<li></li>
</ul>
<p>
©
<a href="https://docs.python.org/3/copyright.html">Copyright</a>
2001-2021, Python Software Foundation.<br />
The Python Software Foundation is a non-profit corporation.
<a href="https://www.python.org/psf/donations/">Please donate.</a>
</p>
<p>
Last updated on May 30, 2021.
<a href="https://docs.python.org/3/bugs.html">Found a bug</a>?<br />
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 2.4.4.
</p>
</body>
</html>