Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Importing a module is different from executing it as a script. If you don't trust the child Python script; you shouldn't run any code from it. </p> <p>A regular way to use some code from another Python module:</p> <pre><code>import another_module result = another_module.some_function(args) </code></pre> <p>If you want to execute it instead of importing:</p> <pre><code>namespace = {'args': [1,2,3]} # define __name__, __file__ if necessary execfile('some_script.py', namespace) result = namespace['result'] </code></pre> <p><a href="http://docs.python.org/2/library/functions.html#execfile" rel="nofollow noreferrer"><code>execfile()</code></a> is used very rarely in Python. It might be useful in a debugger, a profiler, or to run <code>setup.py</code> in tools such as <code>pip</code>, <code>easy_install</code>.</p> <p>See also <a href="http://docs.python.org/2/library/runpy.html" rel="nofollow noreferrer"><code>runpy</code> module</a>.</p> <p>If another script is executed in a different process; you could use many <a href="http://en.wikipedia.org/wiki/Inter-process_communication" rel="nofollow noreferrer">IPC methods</a>. The simplest way is just pipe serialized (Python objects converted to a bytestring) input args into subprocess' stdin and read the result back from its stdout as <a href="https://stackoverflow.com/a/16877343/4279">suggested by @kirelagin</a>:</p> <pre><code>import json import sys from subprocess import Popen, PIPE marshal, unmarshal = json.dumps, json.loads p = Popen([sys.executable, 'some_script.py'], stdin=PIPE, stdout=PIPE) result = unmarshal(p.communicate(marshal(args))[0]) </code></pre> <p>where <code>some_script.py</code> could be:</p> <pre><code>#!/usr/bin/env python import json import sys args = json.load(sys.stdin) # read input data from stdin result = [x*x for x in args] # compute result json.dump(result, sys.stdout) # write result to stdout </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

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