Saving Lua output to the Python string

469 Views Asked by At

I have a Lua expression, which can print some string. For example: print(1+2). Then, I call this expression in Python 3 using Lupa library:

import lupa

lua = lupa.LuaRuntime(unpack_returned_tuples=True)
res = lua.eval('print(1+2)')

Of course, res is None because this expression returns nothing. But I need to catch the output and save in the variable.

Is this possible? How can I do this?

1

There are 1 best solutions below

0
On

I've never used Python, but I think you need to do this:

import lupa

lua = lupa.LuaRuntime(unpack_returned_tuples=True)
res = lua.eval('1+2')

res should now be 3, which you can print from Python. If you want Lua to print it instead, you'll have to use execute:

res = lua.execute('local v = 1+2 print(v) return v')

(Based on similar questions that had those code examples)