-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathinterpreter.html
472 lines (472 loc) · 16.7 KB
/
interpreter.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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
<!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>interpreter</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="introduction.html"
title="3. An Informal Introduction to Python"
>next</a
>
|
</li>
<li>
<a href="appetite.html" title="1. Whetting Your Appetite">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-using"></span></p>
<h1 id="using-the-python-interpreter">
<span class="section-number">2. </span>Using the Python Interpreter<a
href="#using-the-python-interpreter"
class="headerlink"
title="Permalink to this headline"
>¶</a
>
</h1>
<p><span id="tut-invoking"></span></p>
<h2 id="invoking-the-interpreter">
<span class="section-number">2.1. </span>Invoking the Interpreter<a
href="#invoking-the-interpreter"
class="headerlink"
title="Permalink to this headline"
>¶</a
>
</h2>
<p>
The Python interpreter is usually installed as
<code>/usr/local/bin/python3.9</code> on those machines where it is
available; putting <code>/usr/local/bin</code> in your Unix shell’s search
path makes it possible to start it by typing the command:
</p>
<pre><code>python3.9</code></pre>
<p>
to the shell.
<a href="#id2" id="id1" class="footnote-reference brackets">1</a> Since
the choice of the directory where the interpreter lives is an installation
option, other places are possible; check with your local Python guru or
system administrator. (E.g., <code>/usr/local/python</code> is a popular
alternative location.)
</p>
<p>
On Windows machines where you have installed Python from the
<a
href="https://docs.python.org/3/using/windows.html#windows-store"
class="reference internal"
><span class="std std-ref">Microsoft Store</span></a
>, the <code>python3.9</code> command will be available. If you have the
<a
href="https://docs.python.org/3/using/windows.html#launcher"
class="reference internal"
><span class="std std-ref">py.exe launcher</span></a
>
installed, you can use the <code>py</code> command. See
<a
href="https://docs.python.org/3/using/windows.html#setting-envvars"
class="reference internal"
><span class="std std-ref"
>Excursus: Setting environment variables</span
></a
>
for other ways to launch Python.
</p>
<p>
Typing an end-of-file character (Control-D on Unix, Control-Z on Windows)
at the primary prompt causes the interpreter to exit with a zero exit
status. If that doesn’t work, you can exit the interpreter by typing the
following command: <code>quit()</code>.
</p>
<p>
The interpreter’s line-editing features include interactive editing,
history substitution and code completion on systems that support the
<a
href="https://tiswww.case.edu/php/chet/readline/rltop.html"
class="reference external"
>GNU Readline</a
>
library. Perhaps the quickest check to see whether command line editing is
supported is typing Control-P to the first Python prompt you get. If it
beeps, you have command line editing; see Appendix
<a href="interactive.html#tut-interacting" class="reference internal"
><span class="std std-ref"
>Interactive Input Editing and History Substitution</span
></a
>
for an introduction to the keys. If nothing appears to happen, or if
<code>^P</code> is echoed, command line editing isn’t available; you’ll
only be able to use backspace to remove characters from the current line.
</p>
<p>
The interpreter operates somewhat like the Unix shell: when called with
standard input connected to a tty device, it reads and executes commands
interactively; when called with a file name argument or with a file as
standard input, it reads and executes a <em>script</em> from that file.
</p>
<p>
A second way of starting the interpreter is
<code>python -c command [arg] ...</code>, which executes the statement(s)
in <em>command</em>, analogous to the shell’s
<a
href="https://docs.python.org/3/using/cmdline.html#cmdoption-c"
class="reference internal"
><code class="xref std std-option docutils literal notranslate"
>-c</code
></a
>
option. Since Python statements often contain spaces or other characters
that are special to the shell, it is usually advised to quote
<em>command</em> in its entirety with single quotes.
</p>
<p>
Some Python modules are also useful as scripts. These can be invoked using
<code>python -m module [arg] ...</code>, which executes the source file
for <em>module</em> as if you had spelled out its full name on the command
line.
</p>
<p>
When a script file is used, it is sometimes useful to be able to run the
script and enter interactive mode afterwards. This can be done by passing
<a
href="https://docs.python.org/3/using/cmdline.html#cmdoption-i"
class="reference internal"
><code class="xref std std-option docutils literal notranslate"
>-i</code
></a
>
before the script.
</p>
<p>
All command line options are described in
<a
href="https://docs.python.org/3/using/cmdline.html#using-on-general"
class="reference internal"
><span class="std std-ref">Command line and environment</span></a
>.
</p>
<p><span id="tut-argpassing"></span></p>
<h3 id="argument-passing">
<span class="section-number">2.1.1. </span>Argument Passing<a
href="#argument-passing"
class="headerlink"
title="Permalink to this headline"
>¶</a
>
</h3>
<p>
When known to the interpreter, the script name and additional arguments
thereafter are turned into a list of strings and assigned to the
<code>argv</code> variable in the <code>sys</code> module. You can access
this list by executing <code>import sys</code>. The length of the list is
at least one; when no script and no arguments are given,
<code>sys.argv[0]</code> is an empty string. When the script name is given
as <code>'-'</code> (meaning standard input), <code>sys.argv[0]</code> is
set to <code>'-'</code>. When
<a
href="https://docs.python.org/3/using/cmdline.html#cmdoption-c"
class="reference internal"
><code class="xref std std-option docutils literal notranslate"
>-c</code
></a
>
<em>command</em> is used, <code>sys.argv[0]</code> is set to
<code>'-c'</code>. When
<a
href="https://docs.python.org/3/using/cmdline.html#cmdoption-m"
class="reference internal"
><code class="xref std std-option docutils literal notranslate"
>-m</code
></a
>
<em>module</em> is used, <code>sys.argv[0]</code> is set to the full name
of the located module. Options found after
<a
href="https://docs.python.org/3/using/cmdline.html#cmdoption-c"
class="reference internal"
><code class="xref std std-option docutils literal notranslate"
>-c</code
></a
>
<em>command</em> or
<a
href="https://docs.python.org/3/using/cmdline.html#cmdoption-m"
class="reference internal"
><code class="xref std std-option docutils literal notranslate"
>-m</code
></a
>
<em>module</em> are not consumed by the Python interpreter’s option
processing but left in <code>sys.argv</code> for the command or module to
handle.
</p>
<p><span id="tut-interactive"></span></p>
<h3 id="interactive-mode">
<span class="section-number">2.1.2. </span>Interactive Mode<a
href="#interactive-mode"
class="headerlink"
title="Permalink to this headline"
>¶</a
>
</h3>
<p>
When commands are read from a tty, the interpreter is said to be in
<em>interactive mode</em>. In this mode it prompts for the next command
with the <em>primary prompt</em>, usually three greater-than signs
(<code>>>></code>); for continuation lines it prompts with the
<em>secondary prompt</em>, by default three dots (<code>...</code>). The
interpreter prints a welcome message stating its version number and a
copyright notice before printing the first prompt:
</p>
<pre><code>$ python3.9
Python 3.9 (default, June 4 2019, 09:25:04)
[GCC 4.8.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>></code></pre>
<p>
Continuation lines are needed when entering a multi-line construct. As an
example, take a look at this
<a
href="https://docs.python.org/3/reference/compound_stmts.html#if"
class="reference internal"
><code class="xref std std-keyword docutils literal notranslate"
>if</code
></a
>
statement:
</p>
<pre><code>>>> the_world_is_flat = True
>>> if the_world_is_flat:
... print("Be careful not to fall off!")
...
Be careful not to fall off!</code></pre>
<p>
For more on interactive mode, see
<a href="appendix.html#tut-interac" class="reference internal"
><span class="std std-ref">Interactive Mode</span></a
>.
</p>
<p><span id="tut-interp"></span></p>
<h2 id="the-interpreter-and-its-environment">
<span class="section-number">2.2. </span>The Interpreter and Its
Environment<a
href="#the-interpreter-and-its-environment"
class="headerlink"
title="Permalink to this headline"
>¶</a
>
</h2>
<p><span id="tut-source-encoding"></span></p>
<h3 id="source-code-encoding">
<span class="section-number">2.2.1. </span>Source Code Encoding<a
href="#source-code-encoding"
class="headerlink"
title="Permalink to this headline"
>¶</a
>
</h3>
<p>
By default, Python source files are treated as encoded in UTF-8. In that
encoding, characters of most languages in the world can be used
simultaneously in string literals, identifiers and comments — although the
standard library only uses ASCII characters for identifiers, a convention
that any portable code should follow. To display all these characters
properly, your editor must recognize that the file is UTF-8, and it must
use a font that supports all the characters in the file.
</p>
<p>
To declare an encoding other than the default one, a special comment line
should be added as the <em>first</em> line of the file. The syntax is as
follows:
</p>
<pre><code># -*- coding: encoding -*-</code></pre>
<p>
where <em>encoding</em> is one of the valid
<a
href="https://docs.python.org/3/library/codecs.html#module-codecs"
class="reference internal"
title="codecs: Encode and decode data and streams."
><code class="sourceCode python">codecs</code></a
>
supported by Python.
</p>
<p>
For example, to declare that Windows-1252 encoding is to be used, the
first line of your source code file should be:
</p>
<pre><code># -*- coding: cp1252 -*-</code></pre>
<p>
One exception to the <em>first line</em> rule is when the source code
starts with a
<a href="appendix.html#tut-scripts" class="reference internal"
><span class="std std-ref">UNIX “shebang” line</span></a
>. In this case, the encoding declaration should be added as the second
line of the file. For example:
</p>
<pre><code>#!/usr/bin/env python3
# -*- coding: cp1252 -*-</code></pre>
<p>Footnotes</p>
<p>
<span class="brackets"><a href="#id1" class="fn-backref">1</a></span
><br />
On Unix, the Python 3.x interpreter is by default not installed with the
executable named <code>python</code>, so that it does not conflict with a
simultaneously installed Python 2.x executable.
</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"
>2. Using the Python Interpreter</a
>
<ul>
<li>
<a href="#invoking-the-interpreter" class="reference internal"
>2.1. Invoking the Interpreter</a
>
<ul>
<li>
<a href="#argument-passing" class="reference internal"
>2.1.1. Argument Passing</a
>
</li>
<li>
<a href="#interactive-mode" class="reference internal"
>2.1.2. Interactive Mode</a
>
</li>
</ul>
</li>
<li>
<a
href="#the-interpreter-and-its-environment"
class="reference internal"
>2.2. The Interpreter and Its Environment</a
>
<ul>
<li>
<a href="#source-code-encoding" class="reference internal"
>2.2.1. Source Code Encoding</a
>
</li>
</ul>
</li>
</ul>
</li>
</ul>
<h4 id="previous-topic">Previous topic</h4>
<p>
<a href="appetite.html" title="previous chapter"
><span class="section-number">1. </span>Whetting Your Appetite</a
>
</p>
<h4 id="next-topic">Next topic</h4>
<p>
<a href="introduction.html" title="next chapter"
><span class="section-number">3. </span>An Informal Introduction to
Python</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/interpreter.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="introduction.html"
title="3. An Informal Introduction to Python"
>next</a
>
|
</li>
<li>
<a href="appetite.html" title="1. Whetting Your Appetite">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>