lunatic-python icon indicating copy to clipboard operation
lunatic-python copied to clipboard

cannot use python in lua

Open deejayd203 opened this issue 8 years ago • 20 comments

i use a raspberry pi 2 and 3 if i do py = require 'python'

this is what i get

py = require 'python' stdin:1: module 'python' not found: no field package.preload['python'] no file '/usr/local/share/lua/5.2/python.lua' no file '/usr/local/share/lua/5.2/python/init.lua' no file '/usr/local/lib/lua/5.2/python.lua' no file '/usr/local/lib/lua/5.2/python/init.lua' no file '/usr/share/lua/5.2/python.lua' no file '/usr/share/lua/5.2/python/init.lua' no file './python.lua' no file '/usr/local/lib/lua/5.2/python.so' no file '/usr/lib/arm-linux-gnueabihf/lua/5.2/python.so' no file '/usr/lib/lua/5.2/python.so' no file '/usr/local/lib/lua/5.2/loadall.so' no file './python.so' stack traceback: [C]: in function 'require' stdin:1: in main chunk [C]: in ?

any help

deejayd203 avatar Sep 06 '16 11:09 deejayd203

Most likely, your LUA_CPATH has not been configured accordingly.

haianos avatar Oct 06 '16 16:10 haianos

what should we do?

Erf4nDev avatar Mar 25 '17 23:03 Erf4nDev

Two things come to mind, you can either move the python.so into one of the default search paths that Lua looks in(eg. from the stacktrace) or you can add the location where python.so exist to LUA_CPATH(as a shell's env variable). Or you can also append that path to package.cpath from Lua. For example

package.cpath = package.cpath .. ";/path_where_your/lunatic_python_binary_exist/goes_here/?.so"
py = require 'python'
-- ...

greatwolf avatar Mar 25 '17 23:03 greatwolf

thanks i'll check

Erf4nDev avatar Mar 27 '17 12:03 Erf4nDev

Hallo! I set LUA_CPATH as I see libs copied to site-packages export LUA_CPATH="/usr/lib/python2.7/site-packages/?.so;;" also change 'python' -> 'lua-python' $ lua test_py.lua lua: error loading module 'lua-python' from file '/usr/lib/python2.7/site-packages/lua-python.so': /usr/lib/python2.7/site-packages/lua-python.so: undefined symbol: _Py_ZeroStruct stack traceback: [C]: in ? [C]: in function 'require' test_py.lua:1: in main chunk [C]: in ? Press any key to continue...

Weee new issue:) What does it mean? Is "lua-python.so" the right lib to use in lua scripts? Why it is installed in python2.7/site-packages ? What should I do to fix?

Thanks in advance!

aisurfer avatar Jul 19 '17 23:07 aisurfer

My .so files had much longer names. I ended up running into the same issue as you, @surfindominator .

@greatwolf , is there something we're getting wrong? I'd be happy to work with you to figure this out so we can get everyone an answer.

hosford42 avatar Oct 05 '17 02:10 hosford42

FYI, I'm using Python 3.5 on Ubuntu, with virtualenv. I'm curious what environments the other folks having this issue are using. Maybe there is a commonality?

hosford42 avatar Oct 05 '17 02:10 hosford42

What's the name of the lunatic .so built? What's the full path of its location? Also what version of lua/luajit is being used?

Note that lua C modules are sensitive to how the shared library is named. Specifically, the name of the *.so needs to match entry function eg. python.so needs to contain luaopen_python. If the .so file gets renamed you'll also have to update the entry function name too or lua will fail to load it.

greatwolf avatar Oct 05 '17 03:10 greatwolf

I'm using Lua 5.2.4. It builds two .so files:

copying build/lib.linux-x86_64-3.5/lua-python.cpython-35m-x86_64-linux-gnu.so -> /home/hosford4/.virtualenvs/testenv/lib/python3.5/site-packages
copying build/lib.linux-x86_64-3.5/lua.cpython-35m-x86_64-linux-gnu.so -> /home/hosford42/.virtualenvs/testenv/lib/python3.5/site-packages

I tried renaming each of them to python.so, and got the same error as @surfindominator for each file. I'm not sure how to go about renaming the entry function, but it looks like it's getting past that point considering the error message -- or maybe I'm misunderstanding.

hosford42 avatar Oct 06 '17 03:10 hosford42

@greatwolf, I looked a little closer and realized the message is not identical. It's failing to resolve PyFloat_Type, not _Py_ZeroStruct. After a bit of googling, I suspect maybe the issue is that libpython is not being linked against.

hosford42 avatar Oct 06 '17 04:10 hosford42

I think I found a bug in the setup.py script. In pkgconfig, the for loop that runs pkg-config breaks if the first call is successful. This means that the lua libs & cflags are captured, but the python libs & cflags are omitted. I modified the function to:

def pkgconfig(*packages):
    # map pkg-config output to kwargs for distutils.core.Extension
    flag_map = {'-I': 'include_dirs', '-L': 'library_dirs', '-l': 'libraries'}

    combined_pcoutput = ''
    for package in packages:
        (pcstatus, pcoutput) = commands.getstatusoutput(
            "pkg-config --libs --cflags %s" % package)
        if pcstatus == 0:
            combined_pcoutput += ' ' + pcoutput
        else:
            sys.exit("pkg-config failed for %s; "
                     "most recent output was:\n%s" %
                     (", ".join(packages), pcoutput))

    kwargs = {}
    for token in combined_pcoutput.split():
        if token[:2] in flag_map:
            kwargs.setdefault(flag_map.get(token[:2]), []).append(token[2:])
        else:                           # throw others to extra_link_args
            kwargs.setdefault('extra_link_args', []).append(token)

    if PY3:
        items = kwargs.items()
    else:
        items = kwargs.iteritems()
    for k, v in items:     # remove duplicated
        kwargs[k] = list(set(v))

    return kwargs

After making this change, and renaming the .so files to lua.so and lua-python.so according to the filename prefixes, I was able to get past the require statement without error.

hosford42 avatar Oct 06 '17 04:10 hosford42

I submitted a pull request for the bug fix. This should resolve the linking issues. The .so naming issues are still outstanding.

hosford42 avatar Oct 06 '17 04:10 hosford42

I'm running into a new issue now. After require executes successfully, the python global is nil. Any idea what might cause this?

hosford42 avatar Oct 06 '17 05:10 hosford42

I'm also getting warnings that python is an undeclared global.

hosford42 avatar Oct 06 '17 05:10 hosford42

I submitted another pull request for the python global being undefined.

hosford42 avatar Oct 06 '17 05:10 hosford42

@greatwolf, it seems that @hosford42 is actively helping in developing lunatic-python. Would you like to have him added as a collaborator?

bastibe avatar Oct 06 '17 06:10 bastibe

@bastibe Sure thing, it would be nice to have multiple maintainers ;)

greatwolf avatar Oct 06 '17 18:10 greatwolf

Well then, @hosford42, welcome to the team!

This comes with no obligations at all, but I am not the author of this package either. Think of it as a "community wiki" post on StackOverflow.

bastibe avatar Oct 07 '17 14:10 bastibe

Thanks, guys! Not sure how much I can help, but happy to when I can.

On 10/07/2017 09:33 AM, Bastian Bechtold wrote:

Well then, @hosford42 https://github.com/hosford42, welcome to the team!

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/bastibe/lunatic-python/issues/41#issuecomment-334939179, or mute the thread https://github.com/notifications/unsubscribe-auth/AEWls6JhisL8L_lcXpDQNrIq7ALN6p6Gks5sp4uzgaJpZM4J1uoH.

hosford42 avatar Oct 07 '17 18:10 hosford42

cp ./build/lib.linux-x86_64-2.7/lua-python.so ./test/python.so lua test_py.lua

primewang avatar Jun 11 '18 06:06 primewang