1 \chapter{Top-level components
\label{top-level
}}
3 The Python interpreter can get its input from a number of sources:
4 from a script passed to it as standard input or as program argument,
5 typed in interactively, from a module source file, etc. This chapter
6 gives the syntax used in these cases.
10 \section{Complete Python programs
\label{programs
}}
13 While a language specification need not prescribe how the language
14 interpreter is invoked, it is useful to have a notion of a complete
15 Python program. A complete Python program is executed in a minimally
16 initialized environment: all built-in and standard modules are
17 available, but none have been initialized, except for
\module{sys
}
18 (various system services),
\module{__builtin__
} (built-in functions,
19 exceptions and
\code{None
}) and
\module{__main__
}. The latter is used
20 to provide the local and global namespace for execution of the
23 \refbimodindex{__main__
}
24 \refbimodindex{__builtin__
}
26 The syntax for a complete Python program is that for file input,
27 described in the next section.
29 The interpreter may also be invoked in interactive mode; in this case,
30 it does not read and execute a complete program but reads and executes
31 one statement (possibly compound) at a time. The initial environment
32 is identical to that of a complete program; each statement is executed
33 in the namespace of
\module{__main__
}.
34 \index{interactive mode
}
35 \refbimodindex{__main__
}
37 Under
{\UNIX}, a complete program can be passed to the interpreter in
38 three forms: with the
\programopt{-c
} \var{string
} command line option, as a
39 file passed as the first command line argument, or as standard input.
40 If the file or standard input is a tty device, the interpreter enters
41 interactive mode; otherwise, it executes the file as a complete
45 \index{standard input
}
48 \section{File input
\label{file-input
}}
50 All input read from non-interactive files has the same form:
52 \begin{productionlist
}
53 \production{file_input
}
54 {(NEWLINE |
\token{statement
})*
}
57 This syntax is used in the following situations:
61 \item when parsing a complete Python program (from a file or from a string);
63 \item when parsing a module;
65 \item when parsing a string passed to the
\keyword{exec
} statement;
70 \section{Interactive input
\label{interactive
}}
72 Input in interactive mode is parsed using the following grammar:
74 \begin{productionlist
}
75 \production{interactive_input
}
76 {[\token{stmt_list
}] NEWLINE |
\token{compound_stmt
} NEWLINE
}
79 Note that a (top-level) compound statement must be followed by a blank
80 line in interactive mode; this is needed to help the parser detect the
84 \section{Expression input
\label{expression-input
}}
87 There are two forms of expression input. Both ignore leading
89 The string argument to
\function{eval()
} must have the following form:
92 \begin{productionlist
}
93 \production{eval_input
}
94 {\token{expression_list
} NEWLINE*
}
97 The input line read by
\function{input()
} must have the following form:
100 \begin{productionlist
}
101 \production{input_input
}
102 {\token{expression_list
} NEWLINE
}
105 Note: to read `raw' input line without interpretation, you can use the
106 built-in function
\function{raw_input()
} or the
\method{readline()
} method
111 \bifuncindex{raw_input
}
112 \withsubitem{(file method)
}{\ttindex{readline()
}}