<div class="section" id="the-exec-statement">
<span id="exec"></span><h2>The <a class="reference internal" href="#exec"><tt class="xref docutils literal"><span class="pre">exec</span></tt></a> statement</h2>
<pre id="index-1068">
<strong id="grammar-token-exec_stmt">exec_stmt</strong> ::=  &quot;exec&quot; <a class="reference external" href="expressions.html#grammar-token-or_expr"><tt class="xref docutils literal"><span class="pre">or_expr</span></tt></a> [&quot;in&quot; <a class="reference external" href="expressions.html#grammar-token-expression"><tt class="xref docutils literal"><span class="pre">expression</span></tt></a> [&quot;,&quot; <a class="reference external" href="expressions.html#grammar-token-expression"><tt class="xref docutils literal"><span class="pre">expression</span></tt></a>]]
</pre>
<p>This statement supports dynamic execution of Python code.  The first expression
should evaluate to either a string, an open file object, or a code object.  If
it is a string, the string is parsed as a suite of Python statements which is
then executed (unless a syntax error occurs). <a class="footnote-reference" href="#id3" id="id2">[1]</a>  If it is an open file, the file
is parsed until EOF and executed.  If it is a code object, it is simply
executed.  In all cases, the code that&#8217;s executed is expected to be valid as
file input (see section <a class="reference external" href="toplevel_components.html#file-input"><em>File input</em></a>).  Be aware that the
<a class="reference internal" href="#return"><tt class="xref docutils literal"><span class="pre">return</span></tt></a> and <a class="reference internal" href="#yield"><tt class="xref docutils literal"><span class="pre">yield</span></tt></a> statements may not be used outside of
function definitions even within the context of code passed to the
<a class="reference internal" href="#exec"><tt class="xref docutils literal"><span class="pre">exec</span></tt></a> statement.</p>
<p>In all cases, if the optional parts are omitted, the code is executed in the
current scope.  If only the first expression after <a class="reference external" href="expressions.html#in"><tt class="xref docutils literal"><span class="pre">in</span></tt></a> is specified,
it should be a dictionary, which will be used for both the global and the local
variables.  If two expressions are given, they are used for the global and local
variables, respectively. If provided, <em>locals</em> can be any mapping object.</p>
<p class="versionchanged">
<span class="versionmodified">Changed in version 2.4: </span>Formerly, <em>locals</em> was required to be a dictionary.</p>
<p id="index-1069">As a side effect, an implementation may insert additional keys into the
dictionaries given besides those corresponding to variable names set by the
executed code.  For example, the current implementation may add a reference to
the dictionary of the built-in module <a title="The module that provides the built-in namespace." class="reference external" href="../library/__builtin__.html#module-__builtin__"><tt class="xref docutils literal"><span class="pre">__builtin__</span></tt></a> under the key
<tt class="docutils literal"><span class="pre">__builtins__</span></tt> (!).</p>
<p id="index-1070"><strong>Programmer&#8217;s hints:</strong> dynamic evaluation of expressions is supported by the
built-in function <a title="eval" class="reference external" href="../library/functions.html#eval"><tt class="xref docutils literal"><span class="pre">eval()</span></tt></a>.  The built-in functions <a title="globals" class="reference external" href="../library/functions.html#globals"><tt class="xref docutils literal"><span class="pre">globals()</span></tt></a> and
<a title="locals" class="reference external" href="../library/functions.html#locals"><tt class="xref docutils literal"><span class="pre">locals()</span></tt></a> return the current global and local dictionary, respectively,
which may be useful to pass around for use by <a class="reference internal" href="#exec"><tt class="xref docutils literal"><span class="pre">exec</span></tt></a>.</p>
<p class="rubric">Footnotes</p>
<table class="docutils footnote" frame="void" id="id3" rules="none">
<colgroup><col class="label" /><col /></colgroup>
<tbody valign="top">
<tr><td class="label"><a class="fn-backref" href="#id2">[1]</a></td><td>Note that the parser only accepts the Unix-style end of line convention.
If you are reading the code from a file, make sure to use universal
newline mode to convert Windows or Mac-style newlines.</td></tr>
</tbody>
</table>
</div>
</div>

