|
| 1 | +### Navigation |
| 2 | + |
| 3 | +- [index](https://docs.python.org/3/genindex.html "General Index") |
| 4 | +- [modules](https://docs.python.org/3/py-modindex.html "Python Module Index") | |
| 5 | +- [next](https://docs.python.org/3/using/index.html "Python Setup and Usage") | |
| 6 | +- [previous](floatingpoint.html "15. Floating Point Arithmetic: Issues and Limitations") | |
| 7 | +-  |
| 8 | +- [Python](https://www.python.org/) » |
| 9 | +- [3.9.5 Documentation](https://docs.python.org/3/index.html) » |
| 10 | +- [The Python Tutorial](index.html) » |
| 11 | +- |
| 12 | + |
| 13 | + | |
| 14 | + |
| 15 | +<span id="tut-appendix"></span> |
| 16 | + |
| 17 | +<span class="section-number">16. </span>Appendix<a href="#appendix" class="headerlink" title="Permalink to this headline">¶</a> |
| 18 | +=============================================================================================================================== |
| 19 | + |
| 20 | +<span id="tut-interac"></span> |
| 21 | + |
| 22 | +<span class="section-number">16.1. </span>Interactive Mode<a href="#interactive-mode" class="headerlink" title="Permalink to this headline">¶</a> |
| 23 | +------------------------------------------------------------------------------------------------------------------------------------------------- |
| 24 | + |
| 25 | +<span id="tut-error"></span> |
| 26 | + |
| 27 | +### <span class="section-number">16.1.1. </span>Error Handling<a href="#error-handling" class="headerlink" title="Permalink to this headline">¶</a> |
| 28 | + |
| 29 | +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. |
| 30 | + |
| 31 | +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. |
| 32 | + |
| 33 | +<span id="tut-scripts"></span> |
| 34 | + |
| 35 | +### <span class="section-number">16.1.2. </span>Executable Python Scripts<a href="#executable-python-scripts" class="headerlink" title="Permalink to this headline">¶</a> |
| 36 | + |
| 37 | +On BSD’ish Unix systems, Python scripts can be made directly executable, like shell scripts, by putting the line |
| 38 | + |
| 39 | + #!/usr/bin/env python3.5 |
| 40 | + |
| 41 | +(assuming that the interpreter is on the user’s <span id="index-0" class="target"></span>`PATH`) at the beginning of the script and giving the file an executable mode. The `#!` must be the first two characters of the file. On some platforms, this first line must end with a Unix-style line ending (`'\n'`), not a Windows (`'\r\n'`) line ending. Note that the hash, or pound, character, `'#'`, is used to start a comment in Python. |
| 42 | + |
| 43 | +The script can be given an executable mode, or permission, using the **chmod** command. |
| 44 | + |
| 45 | + $ chmod +x myscript.py |
| 46 | + |
| 47 | +On Windows systems, there is no notion of an “executable mode”. The Python installer automatically associates `.py` files with `python.exe` so that a double-click on a Python file will run it as a script. The extension can also be `.pyw`, in that case, the console window that normally appears is suppressed. |
| 48 | + |
| 49 | +<span id="tut-startup"></span> |
| 50 | + |
| 51 | +### <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> |
| 52 | + |
| 53 | +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 `.profile` feature of the Unix shells. |
| 54 | + |
| 55 | +This file is only read in interactive sessions, not when Python reads commands from a script, and not when `/dev/tty` 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 `sys.ps1` and `sys.ps2` in this file. |
| 56 | + |
| 57 | +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 `if os.path.isfile('.pythonrc.py'): exec(open('.pythonrc.py').read())`. If you want to use the startup file in a script, you must do this explicitly in the script: |
| 58 | + |
| 59 | + import os |
| 60 | + filename = os.environ.get('PYTHONSTARTUP') |
| 61 | + if filename and os.path.isfile(filename): |
| 62 | + with open(filename) as fobj: |
| 63 | + startup_file = fobj.read() |
| 64 | + exec(startup_file) |
| 65 | + |
| 66 | +<span id="tut-customize"></span> |
| 67 | + |
| 68 | +### <span class="section-number">16.1.4. </span>The Customization Modules<a href="#the-customization-modules" class="headerlink" title="Permalink to this headline">¶</a> |
| 69 | + |
| 70 | +Python provides two hooks to let you customize it: `sitecustomize` and `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: |
| 71 | + |
| 72 | + >>> import site |
| 73 | + >>> site.getusersitepackages() |
| 74 | + '/home/user/.local/lib/python3.5/site-packages' |
| 75 | + |
| 76 | +Now you can create a file named `usercustomize.py` 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. |
| 77 | + |
| 78 | +`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 `usercustomize`. 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. |
| 79 | + |
| 80 | +Footnotes |
| 81 | + |
| 82 | + <span class="brackets"><a href="#id1" class="fn-backref">1</a></span> |
| 83 | +A problem with the GNU Readline package may prevent this. |
| 84 | + |
| 85 | +### [Table of Contents](https://docs.python.org/3/contents.html) |
| 86 | + |
| 87 | +- <a href="#" class="reference internal">16. Appendix</a> |
| 88 | + - <a href="#interactive-mode" class="reference internal">16.1. Interactive Mode</a> |
| 89 | + - <a href="#error-handling" class="reference internal">16.1.1. Error Handling</a> |
| 90 | + - <a href="#executable-python-scripts" class="reference internal">16.1.2. Executable Python Scripts</a> |
| 91 | + - <a href="#the-interactive-startup-file" class="reference internal">16.1.3. The Interactive Startup File</a> |
| 92 | + - <a href="#the-customization-modules" class="reference internal">16.1.4. The Customization Modules</a> |
| 93 | + |
| 94 | +#### Previous topic |
| 95 | + |
| 96 | +[<span class="section-number">15. </span>Floating Point Arithmetic: Issues and Limitations](floatingpoint.html "previous chapter") |
| 97 | + |
| 98 | +#### Next topic |
| 99 | + |
| 100 | +[Python Setup and Usage](https://docs.python.org/3/using/index.html "next chapter") |
| 101 | + |
| 102 | +### This Page |
| 103 | + |
| 104 | +- [Report a Bug](https://docs.python.org/3/bugs.html) |
| 105 | +- [Show Source](https://github.com/python/cpython/blob/3.9/Doc/tutorial/appendix.rst) |
| 106 | + |
| 107 | +### Navigation |
| 108 | + |
| 109 | +- [index](https://docs.python.org/3/genindex.html "General Index") |
| 110 | +- [modules](https://docs.python.org/3/py-modindex.html "Python Module Index") | |
| 111 | +- [next](https://docs.python.org/3/using/index.html "Python Setup and Usage") | |
| 112 | +- [previous](floatingpoint.html "15. Floating Point Arithmetic: Issues and Limitations") | |
| 113 | +-  |
| 114 | +- [Python](https://www.python.org/) » |
| 115 | +- [3.9.5 Documentation](https://docs.python.org/3/index.html) » |
| 116 | +- [The Python Tutorial](index.html) » |
| 117 | +- |
| 118 | + |
| 119 | + | |
| 120 | + |
| 121 | +© [Copyright](https://docs.python.org/3/copyright.html) 2001-2021, Python Software Foundation. |
| 122 | +The Python Software Foundation is a non-profit corporation. [Please donate.](https://www.python.org/psf/donations/) |
| 123 | + |
| 124 | +Last updated on May 30, 2021. [Found a bug](https://docs.python.org/3/bugs.html)? |
| 125 | +Created using [Sphinx](https://www.sphinx-doc.org/) 2.4.4. |
0 commit comments