简体   繁体   English

如何使用 Luarocks 将模块安装到 Luajit

[英]How to install modules to Luajit using Luarocks

I managed to install luasocket and works fine using lua5.3 but i can't find the way to install them to require from luajit.我设法安装了 luasocket 并且使用 lua5.3 工作正常,但我找不到从 luajit 安装它们的方法。

If i write print(_VERSION) it shows luajit use lua5.1.如果我写 print(_VERSION) 它显示 luajit 使用 lua5.1。

This is the stuff i been trying:这是我一直在尝试的东西:

luarocks install luasocket

luarocks --local install luasocket

luarocks --lua-version 5.1 install luasocket

luarocks --lua-version 5.1 --local install luasocket

luarocks config lua_version 5.1

luarocks install luasocket

sock = require("/data/data/com.termux/files/home/.luarocks/lib/luarocks/rocks-5.1/socket")

package.path = package.path .. ";/data/data/com.termux/files/home/.luarocks/lib/luarocks/rocks-5.1" require "socket"

package.path = package.path .. ";/data/data/com.termux/files/home/.luarocks/lib/lua/5.1" require "socket"

...

The error:错误:

$ luajit
LuaJIT 2.1.0-beta3 -- Copyright (C) 2005-2017 Mike Pall. http://luajit.org/
JIT: ON ARMv7 VFPv3 fold cse dce fwd dse narrow loop abc sink fuse
> sock = require("socket")
stdin:1: module 'socket' not found:
        no field package.preload['socket']
        no file './socket.lua'
        no file '/data/data/com.termux/files/usr/share/luajit-2.1.0-beta3/socket.lua'
        no file '/usr/local/share/lua/5.1/socket.lua'
        no file '/usr/local/share/lua/5.1/socket/init.lua'
        no file '/data/data/com.termux/files/usr/share/lua/5.1/socket.lua'
        no file '/data/data/com.termux/files/usr/share/lua/5.1/socket/init.lua'
        no file './socket.so'
        no file '/usr/local/lib/lua/5.1/socket.so'
        no file '/data/data/com.termux/files/usr/lib/lua/5.1/socket.so'
        no file '/usr/local/lib/lua/5.1/loadall.so'
stack traceback:
        [C]: in function 'require'
        stdin:1: in main chunk
        [C]: at 0xaba4406d

I tryied to move the installed files to some of those paths but still don't work.我尝试将已安装的文件移动到其中一些路径,但仍然无法正常工作。

socket.lua was installed to a directory that doesn't exist in your LUA_PATH socket.lua 已安装到 LUA_PATH 中不存在的目录中

find where socket.lua was installed, with find or mlocate, if installed:使用 find 或 mlocate 查找 socket.lua 的安装位置(如果已安装):

find / -name socket.lua 2>&1 | grep -v Perm

sudo updatedb  &&  locate socket.lua

~/.luaver/luarocks/3.2.0_5.1/share/lua/5.1/socket.lua
~/.luaver/luarocks/3.2.0_5.3/share/lua/5.3/socket.lua
/usr/share/lua/5.1/socket.lua
/usr/share/lua/5.2/socket.lua
/usr/share/lua/5.3/socket.lua

edit your .bashrc -- assuming a bash shell here, but others follow a similar process.编辑您的 .bashrc —— 假设这里有一个 bash shell,但其他人遵循类似的过程。 take your pick, and modify accordingly https://wiki.termux.com/wiki/Shells选择,并相应地修改https://wiki.termux.com/wiki/Shells

find the line that says export LUA_PATH='/usr/local/share/lua/5.1/?.lua;yadda-yadda找到说export LUA_PATH='/usr/local/share/lua/5.1/?.lua;yadda-yadda

add a semicolon plus the path(s) you found from above.添加一个分号加上您从上面找到的路径。

export LUA_PATH='/usr/local/share/lua/5.1/?.lua;/usr/share/lua/5.1'

close any open terminals, then open a fresh one, now that you have that new path enabled.关闭所有打开的终端,然后打开一个新的终端,现在您已启用该新路径。


alternatively, you can use softlinks.或者,您可以使用软链接。

ln -s /usr/share/lua/5.1/socket.lua /usr/local/share/lua/5.1/socket.lua

I'm not a fan of this secondary softlink method, because you'd have to do it for every Lua module that resides outside of your path, but I'm including it so you know your options.我不喜欢这种辅助软链接方法,因为您必须为位于路径之外的每个 Lua 模块执行此操作,但我将其包含在内,以便您了解您的选择。 If it's just for that particular module, this would be an acceptable solution, because it does what you set out to do.如果它只是针对那个特定的模块,这将是一个可以接受的解决方案,因为它完成了你想要做的事情。 However, in the future when you run into the same issue with other modules, you would have to repeat the process for those, as well.但是,将来当您遇到其他模块的相同问题时,您也必须为这些模块重复该过程。 Modifying the LUA_PATH is your best bet.修改 LUA_PATH 是您最好的选择。

luarocks --lua-version 5.1 install luasocket

is the proper way to install it.是安装它的正确方法。

That should also tell you in the last line of output where it installed the module, so you can check if that's in your package.path .这也应该在输出的最后一行告诉您它安装模块的位置,因此您可以检查它是否在您的package.path


It seems you just misunderstand how package.path works.看来您只是误解了package.path工作原理。 From the manual :手册

A path is a string containing a sequence of templates separated by semicolons.路径是包含由分号分隔的模板序列的字符串。 For each template, the function replaces each interrogation mark (if any) in the template with a copy of name wherein all occurrences of sep (a dot, by default) were replaced by rep (the system's directory separator, by default), and then tries to open the resulting file name.对于每个模板,该函数将模板中的每个询问标记(如果有)替换为 name 的副本,其中所有出现的 sep(默认情况下为点)都替换为 rep(默认情况下系统的目录分隔符),然后尝试打开生成的文件名。

What that means is that a path like /usr/share/lua/5.1 won't find anything, because it doesn't find a valid Lua file at that path and since there's no ?这意味着像/usr/share/lua/5.1这样的路径不会找到任何东西,因为它在该路径上找不到有效的 Lua 文件,并且因为没有? , no substitution is done whatsoever. ,不进行任何替换。 The first of the two paths is OK, but many Lua modules use a file called init.lua instead, so for every <path>/?.lua you should always also add a <path>/?/init.lua to cover that option as well.两个路径中的第一个是可以的,但是许多 Lua 模块使用一个名为init.lua的文件init.lua代替,因此对于每个<path>/?.lua您还应该始终添加一个<path>/?/init.lua来覆盖它选项。

Normally Luarocks should install the packages main Lua file at /usr/local/share/lua/5.1/socket.lua , but for your case that doesn't seem to work.通常Luarocks 应该在/usr/local/share/lua/5.1/socket.lua安装包主 Lua 文件,但对于您的情况,这似乎不起作用。 You can have a look at that directory and see if the file is there at all, and check that luarocks config deploy_lua_dir actually returns /usr/local/share/lua/5.1 .您可以查看该目录并查看该文件是否存在,并检查luarocks config deploy_lua_dir实际返回/usr/local/share/lua/5.1

Here's the thing, you stated:事情是这样的,你说:

"I add the path and get: /data/data/com.termux/files/home/.luarocks/share/lua/5.1/ socket.lua module 'socket.core' not found..." “我添加路径并得到: /data/data/com.termux/files/home/.luarocks/share/lua/5.1/socket.lua module 'socket.core' not found...”

That's because socket.lua exists in that directory, but you moved it's subdirectory:那是因为 socket.lua 存在于该目录中,但您移动了它的子目录:

/data/data/com.termux/files/home/.luarocks/share/lua/5.1/ socket/core.lua /data/data/com.termux/files/home/.luarocks/share/lua/5.1/socket/core.lua

So that dir will no longer do anything for you until you delete it and begin again.因此,该目录将不再为您做任何事情,直到您将其删除并重新开始。

rm /data/data/com.termux/files/home/.luarocks/share/lua/5.1/socket.lua
rm -rf /data/data/com.termux/files/home/.luarocks/share/lua/5.1/socket/

luarocks --lua-version 5.1 install luasocket

Now watch carefully as it installs, to see what directory it actually installs to, or use that find command I mentioned earlier to locate your Lua5.1 compatable socket location and add that to your LUA_PATH .现在仔细观察它的安装,看看它实际安装到哪个目录,或者使用我之前提到的find命令来定位你的 Lua5.1 兼容套接字位置并将其添加到你的 LUA_PATH

sudo apt install mlocate  &&  sudo updatedb  &&  locate socket.lua

You don't need to add these directories to your LUA_PATH as they're already being searched您不需要将这些目录添加到 LUA_PATH 中,因为它们已经被搜索

> sock = require("socket")
stdin:1: module 'socket' not found:
        no field package.preload['socket']
        no file './socket.lua'
        no file '/data/data/com.termux/files/usr/share/luajit-2.1.0-beta3/socket.lua'
        no file '/usr/local/share/lua/5.1/socket.lua'
        no file '/usr/local/share/lua/5.1/socket/init.lua'
        no file '/data/data/com.termux/files/usr/share/lua/5.1/socket.lua'
        no file '/data/data/com.termux/files/usr/share/lua/5.1/socket/init.lua'
        no file './socket.so'
        no file '/usr/local/lib/lua/5.1/socket.so'
        no file '/data/data/com.termux/files/usr/lib/lua/5.1/socket.so'
        no file '/usr/local/lib/lua/5.1/loadall.so'

;; won't do anything because you've already pointed out that it doesn't reside in any of those default dirs.不会做任何事情,因为您已经指出它不存在于任何这些默认目录中。

It is possible that it's looking for the right dir, but the wrong extension.它可能正在寻找正确的目录,但有错误的扩展名。

/usr/local/lib/lua/5.1/socket. /usr/local/lib/lua/5.1/socket. so所以
as opposed to /usr/local/lib/lua/5.1/socket.与 /usr/local/lib/lua/5.1/socket 相对。 lua卢阿

in this case, use the softlink method I mentioned earlier, making sure to use the correct location, this is just an example:在这种情况下,使用我前面提到的软链接方法,确保使用正确的位置,这只是一个例子:

ln -s /usr/local/lib/lua/5.1/socket.lua /usr/local/lib/lua/5.1/socket.so

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM