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?

0

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

2 Comments

What if I need to open a file? I have a large Lua script and I want to execute it in Python using lupa
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.