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