2

I have some sample of lua code. For example:

for i=1,4 do
   print(i)
end

and I need to execute it using Lupa library on Python 3.4.

For doing this, I have write the following code:

import lupa
lua = lupa.LuaRuntime(unpack_returned_tuples=True)
lua.eval(open('test.lua').read())

where test.lua is a my sample lua file.

But if I try to execute this code, I get following:

Traceback (most recent call last):
  File "/Job/HW-2/testing/test1.py", line 3, in <module>
    lua.eval(open('test.lua').read())
  File "lupa/_lupa.pyx", line 251, in lupa._lupa.LuaRuntime.eval (lupa/_lupa.c:4579)
  File "lupa/_lupa.pyx", line 1250, in lupa._lupa.run_lua (lupa/_lupa.c:18295)
lupa._lupa.LuaSyntaxError: error loading code: [string "<python>"]:1: unexpected symbol near 'for'

It seems, this is a very simple issue, but I don't know how to fix it. So, how I can execute Lua file on Python 3 using Lupa?

VeLKerr
  • 2,995
  • 3
  • 24
  • 47

1 Answers1

3

The eval method requires a Lua expression that returns a value. In order to execute Lua statements, call execute instead:

>>> lua.execute("""for i=1,4 do
...    print(i)
... end""") 
1
2
3
4
Mark Reed
  • 91,912
  • 16
  • 138
  • 175
  • What if I need to open a file? I have a large Lua script and I want to execute it in Python using lupa – Romaldowoho Apr 04 '16 at 01:36
  • Where you get the Lua code has nothing to do with how you execute it. Notice that the question has `open('test.lua').read()` rather than a string literal. – Mark Reed Apr 04 '16 at 11:43