8 .. index:: single: interpreter
10 The Python interpreter can get its input from a number of sources: from a script
11 passed to it as standard input or as program argument, typed in interactively,
12 from a module source file, etc. This chapter gives the syntax used in these
18 Complete Python programs
19 ========================
21 .. index:: single: program
28 While a language specification need not prescribe how the language interpreter
29 is invoked, it is useful to have a notion of a complete Python program. A
30 complete Python program is executed in a minimally initialized environment: all
31 built-in and standard modules are available, but none have been initialized,
32 except for :mod:`sys` (various system services), :mod:`__builtin__` (built-in
33 functions, exceptions and ``None``) and :mod:`__main__`. The latter is used to
34 provide the local and global namespace for execution of the complete program.
36 The syntax for a complete Python program is that for file input, described in
40 single: interactive mode
43 The interpreter may also be invoked in interactive mode; in this case, it does
44 not read and execute a complete program but reads and executes one statement
45 (possibly compound) at a time. The initial environment is identical to that of
46 a complete program; each statement is executed in the namespace of
52 single: standard input
54 Under Unix, a complete program can be passed to the interpreter in three forms:
55 with the :option:`-c` *string* command line option, as a file passed as the
56 first command line argument, or as standard input. If the file or standard input
57 is a tty device, the interpreter enters interactive mode; otherwise, it executes
58 the file as a complete program.
66 All input read from non-interactive files has the same form:
69 file_input: (NEWLINE | `statement`)*
71 This syntax is used in the following situations:
73 * when parsing a complete Python program (from a file or from a string);
75 * when parsing a module;
77 * when parsing a string passed to the :keyword:`exec` statement;
85 Input in interactive mode is parsed using the following grammar:
88 interactive_input: [`stmt_list`] NEWLINE | `compound_stmt` NEWLINE
90 Note that a (top-level) compound statement must be followed by a blank line in
91 interactive mode; this is needed to help the parser detect the end of the input.
99 .. index:: single: input
101 .. index:: builtin: eval
103 There are two forms of expression input. Both ignore leading whitespace. The
104 string argument to :func:`eval` must have the following form:
107 eval_input: `expression_list` NEWLINE*
109 .. index:: builtin: input
111 The input line read by :func:`input` must have the following form:
114 input_input: `expression_list` NEWLINE
121 single: readline() (file method)
123 Note: to read 'raw' input line without interpretation, you can use the built-in
124 function :func:`raw_input` or the :meth:`readline` method of file objects.