Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If you explain why you want to do this, I'm willing to bet that it's not actually the way you should be solving the problem.</p> <p>But meanwhile:</p> <p>The <a href="http://docs.python.org/2/library/functions.html#execfile" rel="nofollow"><code>execfile</code></a> function does exactly what you want: it reads text from a file and executes it as Python code.</p> <p>If you want to read the text into a variable first, and then execute the code in that variable, what you want is the <a href="http://docs.python.org/2/reference/simple_stmts.html#exec" rel="nofollow"><code>exec</code></a> statement.</p> <p>You might instead want a custom importer, so you can just <code>import</code> a module from an HTTP URL, or from stored text. Or, of course, you can always just write the text out to a <code>tempfile.NamedTemporaryFile</code> inside some directory you've added to <code>sys.path</code> and then <code>import</code> that module normally, without any custom importer code. See <a href="http://docs.python.org/2/library/modules.html" rel="nofollow">Importing Modules</a> and <a href="http://www.python.org/dev/peps/pep-0302/" rel="nofollow">PEP 302</a> for all the gory details. (Note that everything related to custom importing is 100x easier in Python 3.3 than 2.7… at the very least because you can find all the docs by starting at <a href="http://docs.python.org/3/reference/import.html#importsystem" rel="nofollow">The import system</a> instead of scattered in 14 places, most of which are comments in C code…)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload