微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

通常当我对远程软件相关的问题有疑问时,我发现其他人已经问过同样的事情,并得到了对我有用的好答案.

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

开始了:
我正在尝试将我的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.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐