Run Lua script from Python -
suppose have lua script contains 2 functions. call each of these functions arguments python script.
i have seen tutorials on how embed lua code in python , vice versa using lunatic python, however, lua functions executed in python script not static , subject change.
hence, need way of importing functions .lua file or executing .lua file python script arguments , receive return value.
could point me in right direction?
would appreciated.
you can use subprocess
run lua script , provide function it's arguments.
import subprocess result = subprocess.check_output(['lua', '-l', 'demo', '-e', 'test("a", "b")']) print(result) result = subprocess.check_output(['lua', '-l', 'demo', '-e', 'test2("a")']) print(result)
- the
-l
requires given library (your script) - the
-e
code should executed on start (your function)
the value of result value of stdout
, write return value , can read in python script. demo lua script used example prints arguments:
function test (a, b) print(a .. ', ' .. b) end function test2(a) print(a) end
in example both files have in same folder , lua
executable must on path
.
an other solution 1 lua vm spawned using pexpect
, run vm in interactive mode.
import pexpect child = pexpect.spawn('lua -i -l demo') child.readline() child.sendline('test("a", "b")') child.readline() print(child.readline()) child.sendline('test2("c")') child.readline() print(child.readline()) child.close()
so can use sendline(...)
send command interpreter , readline()
read output. first child.readline()
after sendline()
reads line command print stdout
.
Comments
Post a Comment