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

cannot use python in lua #41

Open
deejayd203 opened this issue Sep 6, 2016 · 20 comments
Open

cannot use python in lua #41

deejayd203 opened this issue Sep 6, 2016 · 20 comments
Labels

Comments

@deejayd203
Copy link

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

@haianos
Copy link

haianos commented Oct 6, 2016

Most likely, your LUA_CPATH has not been configured accordingly.

@Erf4nDev
Copy link

what should we do?

@greatwolf
Copy link
Collaborator

greatwolf commented Mar 25, 2017

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'
-- ...

@Erf4nDev
Copy link

thanks i'll check

@aisurfer
Copy link

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!

@hosford42
Copy link
Collaborator

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
Copy link
Collaborator

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?

@greatwolf
Copy link
Collaborator

greatwolf commented Oct 5, 2017

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.

@hosford42
Copy link
Collaborator

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
Copy link
Collaborator

@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
Copy link
Collaborator

hosford42 commented Oct 6, 2017

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
Copy link
Collaborator

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

@hosford42
Copy link
Collaborator

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

@hosford42
Copy link
Collaborator

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

@hosford42
Copy link
Collaborator

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

@bastibe
Copy link
Owner

bastibe commented Oct 6, 2017

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

@greatwolf
Copy link
Collaborator

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

@bastibe
Copy link
Owner

bastibe commented Oct 7, 2017

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.

@hosford42
Copy link
Collaborator

hosford42 commented Oct 7, 2017 via email

@primewang
Copy link

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

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

No branches or pull requests

8 participants