Linux/Unix > Python Essential Reference, Second Edition > 10. Execution Environment > Interactive Sessions | See All Titles

< BACK | Make Note | Bookmark | CONTINUE >

155117184014003188065099048180054212144238241179195140058238111161105082191049141199181067

### Interactive Sessions

If no program name is given and the standard input to the interpreter is an interactive terminal, Python starts in interactive mode. In this mode, a banner message is printed and the user is presented with a prompt. In addition, the interpreter evaluates the script contained in the `PYTHONSTARTUP` environment variable (if set). This script is evaluated as if part of the input program (that is, it isn’t loaded using an `import` statement). One application of this script might be to read a user configuration file such as `.pythonrc`.

When accepting interactive input, two user prompts appear. The `>>>` prompt appears at the beginning of a new statement; the `...` prompt indicates a statement continuation. For example:

    Python 2.0 (#1, Oct 27 2000, 14:34:45)
    [GCC 2.95.2 19991024 (release)] on sunos5
    Type "copyright", "credits" or "license" for more information..
    >>> for i in range(0,4):
    ...     print i
    ...
    0
    1
    2
    3
    >>>

In customized applications, you can change the prompts by modifying the values of `sys.ps1` and `sys.ps2`.

On some systems, Python may be compiled to use the GNU readline library. If enabled, this library provides command histories, completion, and other additions to Python’s interactive mode. The special key bindings provided by the `readline` library are described in the readline module in Appendix A.

By default, the output of commands issued in interactive mode is generated by printing the output of the built-in `repr()` function on the result. Starting with Python 2.1, this can be changed by setting the variable `sys.displayhook`. For example:

    >>> def my_display(x):
    ...     print "result = %s" % repr(x)
    ...
    >>> sys.displayhook = my_display
    >>> 3+4
    result = 7
    >>>

| Last updated on 3/28/2002 Python Essential Reference, Second Edition, © 2002 New Riders Publishing

< BACK | Make Note | Bookmark | CONTINUE >

© 2002, O'Reilly & Associates, Inc.