17db96d56Sopenharmony_ci.. _tut-appendix: 27db96d56Sopenharmony_ci 37db96d56Sopenharmony_ci******** 47db96d56Sopenharmony_ciAppendix 57db96d56Sopenharmony_ci******** 67db96d56Sopenharmony_ci 77db96d56Sopenharmony_ci 87db96d56Sopenharmony_ci.. _tut-interac: 97db96d56Sopenharmony_ci 107db96d56Sopenharmony_ciInteractive Mode 117db96d56Sopenharmony_ci================ 127db96d56Sopenharmony_ci 137db96d56Sopenharmony_ci.. _tut-error: 147db96d56Sopenharmony_ci 157db96d56Sopenharmony_ciError Handling 167db96d56Sopenharmony_ci-------------- 177db96d56Sopenharmony_ci 187db96d56Sopenharmony_ciWhen an error occurs, the interpreter prints an error message and a stack trace. 197db96d56Sopenharmony_ciIn interactive mode, it then returns to the primary prompt; when input came from 207db96d56Sopenharmony_cia file, it exits with a nonzero exit status after printing the stack trace. 217db96d56Sopenharmony_ci(Exceptions handled by an :keyword:`except` clause in a :keyword:`try` statement 227db96d56Sopenharmony_ciare not errors in this context.) Some errors are unconditionally fatal and 237db96d56Sopenharmony_cicause an exit with a nonzero exit; this applies to internal inconsistencies and 247db96d56Sopenharmony_cisome cases of running out of memory. All error messages are written to the 257db96d56Sopenharmony_cistandard error stream; normal output from executed commands is written to 267db96d56Sopenharmony_cistandard output. 277db96d56Sopenharmony_ci 287db96d56Sopenharmony_ciTyping the interrupt character (usually :kbd:`Control-C` or :kbd:`Delete`) to the primary or 297db96d56Sopenharmony_cisecondary prompt cancels the input and returns to the primary prompt. [#]_ 307db96d56Sopenharmony_ciTyping an interrupt while a command is executing raises the 317db96d56Sopenharmony_ci:exc:`KeyboardInterrupt` exception, which may be handled by a :keyword:`try` 327db96d56Sopenharmony_cistatement. 337db96d56Sopenharmony_ci 347db96d56Sopenharmony_ci 357db96d56Sopenharmony_ci.. _tut-scripts: 367db96d56Sopenharmony_ci 377db96d56Sopenharmony_ciExecutable Python Scripts 387db96d56Sopenharmony_ci------------------------- 397db96d56Sopenharmony_ci 407db96d56Sopenharmony_ciOn BSD'ish Unix systems, Python scripts can be made directly executable, like 417db96d56Sopenharmony_cishell scripts, by putting the line :: 427db96d56Sopenharmony_ci 437db96d56Sopenharmony_ci #!/usr/bin/env python3.5 447db96d56Sopenharmony_ci 457db96d56Sopenharmony_ci(assuming that the interpreter is on the user's :envvar:`PATH`) at the beginning 467db96d56Sopenharmony_ciof the script and giving the file an executable mode. The ``#!`` must be the 477db96d56Sopenharmony_cifirst two characters of the file. On some platforms, this first line must end 487db96d56Sopenharmony_ciwith a Unix-style line ending (``'\n'``), not a Windows (``'\r\n'``) line 497db96d56Sopenharmony_ciending. Note that the hash, or pound, character, ``'#'``, is used to start a 507db96d56Sopenharmony_cicomment in Python. 517db96d56Sopenharmony_ci 527db96d56Sopenharmony_ciThe script can be given an executable mode, or permission, using the 537db96d56Sopenharmony_ci:program:`chmod` command. 547db96d56Sopenharmony_ci 557db96d56Sopenharmony_ci.. code-block:: shell-session 567db96d56Sopenharmony_ci 577db96d56Sopenharmony_ci $ chmod +x myscript.py 587db96d56Sopenharmony_ci 597db96d56Sopenharmony_ciOn Windows systems, there is no notion of an "executable mode". The Python 607db96d56Sopenharmony_ciinstaller automatically associates ``.py`` files with ``python.exe`` so that 617db96d56Sopenharmony_cia double-click on a Python file will run it as a script. The extension can 627db96d56Sopenharmony_cialso be ``.pyw``, in that case, the console window that normally appears is 637db96d56Sopenharmony_cisuppressed. 647db96d56Sopenharmony_ci 657db96d56Sopenharmony_ci 667db96d56Sopenharmony_ci.. _tut-startup: 677db96d56Sopenharmony_ci 687db96d56Sopenharmony_ciThe Interactive Startup File 697db96d56Sopenharmony_ci---------------------------- 707db96d56Sopenharmony_ci 717db96d56Sopenharmony_ciWhen you use Python interactively, it is frequently handy to have some standard 727db96d56Sopenharmony_cicommands executed every time the interpreter is started. You can do this by 737db96d56Sopenharmony_cisetting an environment variable named :envvar:`PYTHONSTARTUP` to the name of a 747db96d56Sopenharmony_cifile containing your start-up commands. This is similar to the :file:`.profile` 757db96d56Sopenharmony_cifeature of the Unix shells. 767db96d56Sopenharmony_ci 777db96d56Sopenharmony_ciThis file is only read in interactive sessions, not when Python reads commands 787db96d56Sopenharmony_cifrom a script, and not when :file:`/dev/tty` is given as the explicit source of 797db96d56Sopenharmony_cicommands (which otherwise behaves like an interactive session). It is executed 807db96d56Sopenharmony_ciin the same namespace where interactive commands are executed, so that objects 817db96d56Sopenharmony_cithat it defines or imports can be used without qualification in the interactive 827db96d56Sopenharmony_cisession. You can also change the prompts ``sys.ps1`` and ``sys.ps2`` in this 837db96d56Sopenharmony_cifile. 847db96d56Sopenharmony_ci 857db96d56Sopenharmony_ciIf you want to read an additional start-up file from the current directory, you 867db96d56Sopenharmony_cican program this in the global start-up file using code like ``if 877db96d56Sopenharmony_cios.path.isfile('.pythonrc.py'): exec(open('.pythonrc.py').read())``. 887db96d56Sopenharmony_ciIf you want to use the startup file in a script, you must do this explicitly 897db96d56Sopenharmony_ciin the script:: 907db96d56Sopenharmony_ci 917db96d56Sopenharmony_ci import os 927db96d56Sopenharmony_ci filename = os.environ.get('PYTHONSTARTUP') 937db96d56Sopenharmony_ci if filename and os.path.isfile(filename): 947db96d56Sopenharmony_ci with open(filename) as fobj: 957db96d56Sopenharmony_ci startup_file = fobj.read() 967db96d56Sopenharmony_ci exec(startup_file) 977db96d56Sopenharmony_ci 987db96d56Sopenharmony_ci 997db96d56Sopenharmony_ci.. _tut-customize: 1007db96d56Sopenharmony_ci 1017db96d56Sopenharmony_ciThe Customization Modules 1027db96d56Sopenharmony_ci------------------------- 1037db96d56Sopenharmony_ci 1047db96d56Sopenharmony_ciPython provides two hooks to let you customize it: :mod:`sitecustomize` and 1057db96d56Sopenharmony_ci:mod:`usercustomize`. To see how it works, you need first to find the location 1067db96d56Sopenharmony_ciof your user site-packages directory. Start Python and run this code:: 1077db96d56Sopenharmony_ci 1087db96d56Sopenharmony_ci >>> import site 1097db96d56Sopenharmony_ci >>> site.getusersitepackages() 1107db96d56Sopenharmony_ci '/home/user/.local/lib/python3.5/site-packages' 1117db96d56Sopenharmony_ci 1127db96d56Sopenharmony_ciNow you can create a file named :file:`usercustomize.py` in that directory and 1137db96d56Sopenharmony_ciput anything you want in it. It will affect every invocation of Python, unless 1147db96d56Sopenharmony_ciit is started with the :option:`-s` option to disable the automatic import. 1157db96d56Sopenharmony_ci 1167db96d56Sopenharmony_ci:mod:`sitecustomize` works in the same way, but is typically created by an 1177db96d56Sopenharmony_ciadministrator of the computer in the global site-packages directory, and is 1187db96d56Sopenharmony_ciimported before :mod:`usercustomize`. See the documentation of the :mod:`site` 1197db96d56Sopenharmony_cimodule for more details. 1207db96d56Sopenharmony_ci 1217db96d56Sopenharmony_ci 1227db96d56Sopenharmony_ci.. rubric:: Footnotes 1237db96d56Sopenharmony_ci 1247db96d56Sopenharmony_ci.. [#] A problem with the GNU Readline package may prevent this. 125