Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 15e4833

Browse files
committed
Issue #16827: Make Interpreter introduction section of the tutorial more
focussed and move advanced section and customization information to a separate file called appendix. Patch credits: Jamayla Wiley, Ya-Ting Huang and James Brewer.
1 parent 2d510e3 commit 15e4833

3 files changed

Lines changed: 128 additions & 110 deletions

File tree

Doc/tutorial/appendix.rst

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

Doc/tutorial/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,4 @@ The :ref:`glossary` is also worth going through.
5656
whatnow.rst
5757
interactive.rst
5858
floatingpoint.rst
59+
appendix.rst

Doc/tutorial/interpreter.rst

Lines changed: 3 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -112,63 +112,15 @@ example, take a look at this :keyword:`if` statement::
112112
Be careful not to fall off!
113113

114114

115+
For more on interactive mode, see :ref:`tut-interac`.
116+
117+
115118
.. _tut-interp:
116119

117120
The Interpreter and Its Environment
118121
===================================
119122

120123

121-
.. _tut-error:
122-
123-
Error Handling
124-
--------------
125-
126-
When an error occurs, the interpreter prints an error message and a stack trace.
127-
In interactive mode, it then returns to the primary prompt; when input came from
128-
a file, it exits with a nonzero exit status after printing the stack trace.
129-
(Exceptions handled by an :keyword:`except` clause in a :keyword:`try` statement
130-
are not errors in this context.) Some errors are unconditionally fatal and
131-
cause an exit with a nonzero exit; this applies to internal inconsistencies and
132-
some cases of running out of memory. All error messages are written to the
133-
standard error stream; normal output from executed commands is written to
134-
standard output.
135-
136-
Typing the interrupt character (usually Control-C or DEL) to the primary or
137-
secondary prompt cancels the input and returns to the primary prompt. [#]_
138-
Typing an interrupt while a command is executing raises the
139-
:exc:`KeyboardInterrupt` exception, which may be handled by a :keyword:`try`
140-
statement.
141-
142-
143-
.. _tut-scripts:
144-
145-
Executable Python Scripts
146-
-------------------------
147-
148-
On BSD'ish Unix systems, Python scripts can be made directly executable, like
149-
shell scripts, by putting the line ::
150-
151-
#! /usr/bin/env python3.4
152-
153-
(assuming that the interpreter is on the user's :envvar:`PATH`) at the beginning
154-
of the script and giving the file an executable mode. The ``#!`` must be the
155-
first two characters of the file. On some platforms, this first line must end
156-
with a Unix-style line ending (``'\n'``), not a Windows (``'\r\n'``) line
157-
ending. Note that the hash, or pound, character, ``'#'``, is used to start a
158-
comment in Python.
159-
160-
The script can be given an executable mode, or permission, using the
161-
:program:`chmod` command::
162-
163-
$ chmod +x myscript.py
164-
165-
On Windows systems, there is no notion of an "executable mode". The Python
166-
installer automatically associates ``.py`` files with ``python.exe`` so that
167-
a double-click on a Python file will run it as a script. The extension can
168-
also be ``.pyw``, in that case, the console window that normally appears is
169-
suppressed.
170-
171-
172124
.. _tut-source-encoding:
173125

174126
Source Code Encoding
@@ -202,67 +154,8 @@ files. The special encoding comment must be in the *first or second* line
202154
within the file.
203155

204156

205-
.. _tut-startup:
206-
207-
The Interactive Startup File
208-
----------------------------
209-
210-
When you use Python interactively, it is frequently handy to have some standard
211-
commands executed every time the interpreter is started. You can do this by
212-
setting an environment variable named :envvar:`PYTHONSTARTUP` to the name of a
213-
file containing your start-up commands. This is similar to the :file:`.profile`
214-
feature of the Unix shells.
215-
216-
.. XXX This should probably be dumped in an appendix, since most people
217-
don't use Python interactively in non-trivial ways.
218-
219-
This file is only read in interactive sessions, not when Python reads commands
220-
from a script, and not when :file:`/dev/tty` is given as the explicit source of
221-
commands (which otherwise behaves like an interactive session). It is executed
222-
in the same namespace where interactive commands are executed, so that objects
223-
that it defines or imports can be used without qualification in the interactive
224-
session. You can also change the prompts ``sys.ps1`` and ``sys.ps2`` in this
225-
file.
226-
227-
If you want to read an additional start-up file from the current directory, you
228-
can program this in the global start-up file using code like ``if
229-
os.path.isfile('.pythonrc.py'): exec(open('.pythonrc.py').read())``.
230-
If you want to use the startup file in a script, you must do this explicitly
231-
in the script::
232-
233-
import os
234-
filename = os.environ.get('PYTHONSTARTUP')
235-
if filename and os.path.isfile(filename):
236-
exec(open(filename).read())
237-
238-
239-
.. _tut-customize:
240-
241-
The Customization Modules
242-
-------------------------
243-
244-
Python provides two hooks to let you customize it: :mod:`sitecustomize` and
245-
:mod:`usercustomize`. To see how it works, you need first to find the location
246-
of your user site-packages directory. Start Python and run this code:
247-
248-
>>> import site
249-
>>> site.getusersitepackages()
250-
'/home/user/.local/lib/python3.2/site-packages'
251-
252-
Now you can create a file named :file:`usercustomize.py` in that directory and
253-
put anything you want in it. It will affect every invocation of Python, unless
254-
it is started with the :option:`-s` option to disable the automatic import.
255-
256-
:mod:`sitecustomize` works in the same way, but is typically created by an
257-
administrator of the computer in the global site-packages directory, and is
258-
imported before :mod:`usercustomize`. See the documentation of the :mod:`site`
259-
module for more details.
260-
261-
262157
.. rubric:: Footnotes
263158

264159
.. [#] On Unix, the Python 3.x interpreter is by default not installed with the
265160
executable named ``python``, so that it does not conflict with a
266161
simultaneously installed Python 2.x executable.
267-
268-
.. [#] A problem with the GNU Readline package may prevent this.

0 commit comments

Comments
 (0)