Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Call python function from lua #68

Open
Martmists-GH opened this issue May 29, 2019 · 5 comments
Open

Call python function from lua #68

Martmists-GH opened this issue May 29, 2019 · 5 comments

Comments

@Martmists-GH
Copy link

Martmists-GH commented May 29, 2019

Is there a way to call a python function from Lua? I've tried the following:

-- main.lua
py = require "python"
my_lib = py.import "my_lib"
print(my_lib.test)  -- nil, so I can't call it.
# my_lib.py
def test():
    return 10
@greatwolf
Copy link
Collaborator

greatwolf commented May 29, 2019

The handle to the module is returned by py.import so you have to access it through that. For example

local py = require "python"
local mylib = py.import "my_lib"
print(mylib.test)
mylib.test()      --> should return 10.0

@Martmists-GH
Copy link
Author

Martmists-GH commented May 29, 2019

when doing that I simply get

main.lua:4: attempt to call field 'test' (a nil value)

I'm using Lua 5.1 and Python 3.7, both x64

@greatwolf
Copy link
Collaborator

I just tested this on Lua 5.3 with Python 3.3 32-bit but I can't reproduce the error. What's the lua and python script that's producing that error?

@Martmists-GH
Copy link
Author

Martmists-GH commented May 30, 2019

Literally the code in my initial post is causing this. I don't think I modified any files before building either. (Aside from marking the luaopen_python as __declspec(dllexport) to export the function on windows)

@greatwolf
Copy link
Collaborator

What does it show when you print(my_lib)?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants