You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

124 lines
4.5 KiB

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