diff options
| author | Craig Jennings <c@cjennings.net> | 2024-04-07 13:41:34 -0500 |
|---|---|---|
| committer | Craig Jennings <c@cjennings.net> | 2024-04-07 13:41:34 -0500 |
| commit | 754bbf7a25a8dda49b5d08ef0d0443bbf5af0e36 (patch) | |
| tree | f1190704f78f04a2b0b4c977d20fe96a828377f1 /devdocs/python~3.12/tutorial%2Fappendix.html | |
new repository
Diffstat (limited to 'devdocs/python~3.12/tutorial%2Fappendix.html')
| -rw-r--r-- | devdocs/python~3.12/tutorial%2Fappendix.html | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/devdocs/python~3.12/tutorial%2Fappendix.html b/devdocs/python~3.12/tutorial%2Fappendix.html new file mode 100644 index 00000000..dda7e109 --- /dev/null +++ b/devdocs/python~3.12/tutorial%2Fappendix.html @@ -0,0 +1,25 @@ + <span id="tut-appendix"></span><h1> Appendix</h1> <section id="interactive-mode"> <span id="tut-interac"></span><h2> +<span class="section-number">16.1. </span>Interactive Mode</h2> <section id="error-handling"> <span id="tut-error"></span><h3> +<span class="section-number">16.1.1. </span>Error Handling</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 class="reference internal" href="../reference/compound_stmts#except"><code>except</code></a> clause in a <a class="reference internal" href="../reference/compound_stmts#try"><code>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 <kbd class="kbd compound docutils literal notranslate"><kbd class="kbd docutils literal notranslate">Control</kbd>-<kbd class="kbd docutils literal notranslate">C</kbd></kbd> or <kbd class="kbd docutils literal notranslate">Delete</kbd>) to the primary or secondary prompt cancels the input and returns to the primary prompt. <a class="footnote-reference brackets" href="#id2" id="id1">1</a> Typing an interrupt while a command is executing raises the <a class="reference internal" href="../library/exceptions#KeyboardInterrupt" title="KeyboardInterrupt"><code>KeyboardInterrupt</code></a> exception, which may be handled by a <a class="reference internal" href="../reference/compound_stmts#try"><code>try</code></a> statement.</p> </section> <section id="executable-python-scripts"> <span id="tut-scripts"></span><h3> +<span class="section-number">16.1.2. </span>Executable Python Scripts</h3> <p>On BSD’ish Unix systems, Python scripts can be made directly executable, like shell scripts, by putting the line</p> <pre data-language="python">#!/usr/bin/env python3.5 +</pre> <p>(assuming that the interpreter is on the user’s <span class="target" id="index-0"></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 class="program">chmod</strong> command.</p> <pre data-language="shell">$ chmod +x myscript.py +</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> </section> <section id="the-interactive-startup-file"> <span id="tut-startup"></span><h3> +<span class="section-number">16.1.3. </span>The Interactive Startup File</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 class="target" id="index-1"></span><a class="reference internal" href="../using/cmdline#envvar-PYTHONSTARTUP"><code>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 data-language="python">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) +</pre> </section> <section id="the-customization-modules"> <span id="tut-customize"></span><h3> +<span class="section-number">16.1.4. </span>The Customization Modules</h3> <p>Python provides two hooks to let you customize it: <span class="target" id="index-2"></span>sitecustomize and <span class="target" id="index-3"></span>usercustomize. 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 data-language="python">>>> import site +>>> site.getusersitepackages() +'/home/user/.local/lib/python3.5/site-packages' +</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 class="reference internal" href="../using/cmdline#cmdoption-s"><code>-s</code></a> option to disable the automatic import.</p> <p><span class="target" id="index-4"></span>sitecustomize 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 <span class="target" id="index-5"></span>usercustomize. See the documentation of the <a class="reference internal" href="../library/site#module-site" title="site: Module responsible for site-specific configuration."><code>site</code></a> module for more details.</p> <h4 class="rubric">Footnotes</h4> <dl class="footnote brackets"> <dt class="label" id="id2"> +<code>1</code> </dt> <dd> +<p>A problem with the GNU Readline package may prevent this.</p> </dd> </dl> </section> </section> <div class="_attribution"> + <p class="_attribution-p"> + © 2001–2023 Python Software Foundation<br>Licensed under the PSF License.<br> + <a href="https://docs.python.org/3.12/tutorial/appendix.html" class="_attribution-link">https://docs.python.org/3.12/tutorial/appendix.html</a> + </p> +</div> |
