cpython / Doc / ref8.tex

\chapter{Top-level components}

The Python interpreter can get its input from a number of sources:
from a script passed to it as standard input or as program argument,
typed in interactively, from a module source file, etc.  This chapter
gives the syntax used in these cases.

\section{Complete Python programs}

While a language specification need not prescribe how the language
interpreter is invoked, it is useful to have a notion of a complete
Python program.  A complete Python program is executed in a minimally
initialized environment: all built-in and standard modules are
available, but none have been initialized, except for \verb\sys\
(various system services), \verb\__builtin__\ (built-in functions,
exceptions and \verb\None\) and \verb\__main__\.  The latter is used
to provide the local and global name space for execution of the
complete program.

The syntax for a complete Python program is that for file input,
described in the next section.

The interpreter may also be invoked in interactive mode; in this case,
it does not read and execute a complete program but reads and executes
one statement (possibly compound) at a time.  The initial environment
is identical to that of a complete program; each statement is executed
in the name space of \verb\__main__\.
\index{interactive mode}

Under {\UNIX}, a complete program can be passed to the interpreter in
three forms: with the {\bf -c} {\it string} command line option, as a
file passed as the first command line argument, or as standard input.
If the file or standard input is a tty device, the interpreter enters
interactive mode; otherwise, it executes the file as a complete
\index{command line}
\index{standard input}

\section{File input}

All input read from non-interactive files has the same form:

file_input:     (NEWLINE | statement)*

This syntax is used in the following situations:


\item when parsing a complete Python program (from a file or from a string);

\item when parsing a module;

\item when parsing a string passed to the \verb\exec\ statement;


\section{Interactive input}

Input in interactive mode is parsed using the following grammar:

interactive_input: [stmt_list] NEWLINE | compound_stmt NEWLINE

Note that a (top-level) compound statement must be followed by a blank
line in interactive mode; this is needed to help the parser detect the
end of the input.

\section{Expression input}

There are two forms of expression input.  Both ignore leading
The string argument to \verb\eval()\ must have the following form:

eval_input:     condition_list NEWLINE*

The input line read by \verb\input()\ must have the following form:

input_input:    condition_list NEWLINE

Note: to read `raw' input line without interpretation, you can use the
built-in function \verb\raw_input()\  or the \verb\readline()\ method
of file objects.
\index{raw input}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.