1
2.. _top-level:
3
4********************
5Top-level components
6********************
7
8.. index:: single: interpreter
9
10The Python interpreter can get its input from a number of sources: from a script
11passed to it as standard input or as program argument, typed in interactively,
12from a module source file, etc.  This chapter gives the syntax used in these
13cases.
14
15
16.. _programs:
17
18Complete Python programs
19========================
20
21.. index:: single: program
22
23.. index::
24   module: sys
25   module: __main__
26   module: builtins
27
28While a language specification need not prescribe how the language interpreter
29is invoked, it is useful to have a notion of a complete Python program.  A
30complete Python program is executed in a minimally initialized environment: all
31built-in and standard modules are available, but none have been initialized,
32except for :mod:`sys` (various system services), :mod:`builtins` (built-in
33functions, exceptions and ``None``) and :mod:`__main__`.  The latter is used to
34provide the local and global namespace for execution of the complete program.
35
36The syntax for a complete Python program is that for file input, described in
37the next section.
38
39.. index::
40   single: interactive mode
41   module: __main__
42
43The interpreter may also be invoked in interactive mode; in this case, it does
44not 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
46a complete program; each statement is executed in the namespace of
47:mod:`__main__`.
48
49.. index::
50   single: UNIX
51   single: Windows
52   single: command line
53   single: standard input
54
55A complete program can be passed to the interpreter
56in three forms: with the :option:`-c` *string* command line option, as a file
57passed as the first command line argument, or as standard input.  If the file
58or standard input is a tty device, the interpreter enters interactive mode;
59otherwise, it executes the file as a complete program.
60
61
62.. _file-input:
63
64File input
65==========
66
67All input read from non-interactive files has the same form:
68
69.. productionlist:: python-grammar
70   file_input: (NEWLINE | `statement`)*
71
72This syntax is used in the following situations:
73
74* when parsing a complete Python program (from a file or from a string);
75
76* when parsing a module;
77
78* when parsing a string passed to the :func:`exec` function;
79
80
81.. _interactive:
82
83Interactive input
84=================
85
86Input in interactive mode is parsed using the following grammar:
87
88.. productionlist:: python-grammar
89   interactive_input: [`stmt_list`] NEWLINE | `compound_stmt` NEWLINE
90
91Note that a (top-level) compound statement must be followed by a blank line in
92interactive mode; this is needed to help the parser detect the end of the input.
93
94
95.. _expression-input:
96
97Expression input
98================
99
100.. index:: single: input
101.. index:: builtin: eval
102
103:func:`eval` is used for expression input.  It ignores leading whitespace. The
104string argument to :func:`eval` must have the following form:
105
106.. productionlist:: python-grammar
107   eval_input: `expression_list` NEWLINE*
108