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.

相关文章

1.github代码实践源代码是lua脚本语言,下载th之后运行thmai...
此文为搬运帖,原帖地址https://www.cnblogs.com/zwywilliam/...
Rime输入法通过定义lua文件,可以实现获取当前时间日期的功能...
localfunctiongenerate_action(params)localscale_action=cc...
2022年1月11日13:57:45 官方:https://opm.openresty.org/官...
在Lua中的table(表),就像c#中的HashMap(哈希表),key和...