加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

Lua:require找不到子模块,但是searchpath成功了吗?

发布时间:2020-12-14 21:43:45 所属栏目:大数据 来源:网络整理
导读:通常当我对远程软件相关的问题有疑问时,我发现其他人已经问过同样的事情,并得到了对我有用的好答案. 但这一次,我未能找到解决困境的答案. 开始了: 我正在尝试将我的Lua编程提升一三级,并希望使用模块.所以,我有一个像这样的结构: main.luafoo/bar.lua 现在
通常当我对远程软件相关的问题有疑问时,我发现其他人已经问过同样的事情,并得到了对我有用的好答案.

但这一次,我未能找到解决困境的答案.

开始了:
我正在尝试将我的Lua编程提升一三级,并希望使用模块.所以,我有一个像这样的结构:

main.lua
foo/bar.lua

现在,在main.lua我做

require("foo.bar")

失败了,

main.lua:1 module 'foo.bar' not found:
     no field package.preload['foo.bar']
     no file 'foo.bar.lua'
     no file 'foo.bar.lua'
     no file 'foo.lua'

好吧,我的package.path可能有问题所以我使用package.searchpath(“foo.bar”,package.path)看看我做错了什么.

问题是package.searchpath将foo.bar解析为foo / bar.lua,这是完全正确的.

正如我所理解的那样,package.searchpath尝试以与require相同的方式查找模块,但在我的情况下似乎有一些som故障.

令我感到奇怪的是错误输出中没有文件’foo.bar.lua’的重复

我误解了require的使用吗?

我正在使用LuaJIT-2.0.0运行我的块

更新:

我正在使用LuaJIT-2.0.0运行我的块< - 这是我的问题的原因,股票Lua-5.2.2表现得像预期的那样

解决方法

package.path = debug.getinfo(1,"S").source:match[[^@?(.*[/])[^/]-$]] .."?.lua;".. package.path
require("foo.bar")

This line causes require to look in the same directory as the
current file when asked to load other files. If you want it to instead
search a directory relative to the current directory,insert the
relative path between ” and ?.lua

以下是需求说明的一部分:

[…] Otherwise require searches for a Lua loader using the path stored in
package.path. If that also fails,it searches for a C loader using the
path stored in package.cpath. If that also fails,it tries an
all-in-one loader (see package.loaders).

package.path的默认路径始终是执行指定脚本的.exe.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读