【问题标题】:Lua require does not work but file is in the traceLua 要求不起作用,但文件在跟踪中
【发布时间】:2021-03-23 21:57:09
【问题描述】:

我试图在 Lua 中要求文件,在一种情况下它可以工作,但是当我想简化更新 LUA PATH 的要求时,找不到文件,但它在跟踪中!

为了重现我的 require 问题,我使用 package.searchpath 函数进行了测试,该函数采用所需的键和参数中的 Lua 路径。

所以代码:

print('MY LUA PATH')
local myLuaPath = "?;?.lua;D:\\Projets\\wow-addon\\HeyThere\\?;D:\\Projets\\wow-addon\\HeyThere\\src\\HeyThere\\?;D:\\Projets\\wow-addon\\HeyThere\\test\\HeyThere\\?"
print(myLuaPath)
print('package search core.add-test')
print(package.searchpath('core.add-test', myLuaPath))
print('package search test.HeyThere.core.add-test')
print(package.searchpath('test.HeyThere.core.add-test', myLuaPath))

结果:

MY LUA PATH
?;?.lua;D:\Projets\wow-addon\HeyThere\?;D:\Projets\wow-addon\HeyThere\src\HeyThere\?;D:\Projets\wow-addon\HeyThere\test\HeyThere\?
package search core.add-test
nil     no file 'core\add-test'
        no file 'core\add-test.lua'
        no file 'D:\Projets\wow-addon\HeyThere\core\add-test'
        no file 'D:\Projets\wow-addon\HeyThere\src\HeyThere\core\add-test'
        no file 'D:\Projets\wow-addon\HeyThere\test\HeyThere\core\add-test'
package search test.HeyThere.core.add-test
test\HeyThere\core\add-test.lua

所以第一次尝试使用 'core.add-test' 应该可以使用 'D:\Projets\wow-addon\HeyThere\test\HeyThere\?'路径中的值但失败...

trace里面有我要的文件!

没有文件'D:\Projets\wow-addon\HeyThere\test\HeyThere\core\add-test'

但使用相同的 LUA PATH 但从父文件夹开始找到文件...第二次测试使用从 'D:\Projets\wow-addon\HeyThere 找到的 'test.HeyThere.core.add-test' \?'

-> test\HeyThere\core\add-test.lua

有人可以向我解释为什么它第一次不起作用吗?

编辑:

我的当前目录是 D:\Projets\wow-addon\HeyThere

我的 lua.exe 位于 D:\Projets\wow-addon\HeyThere\bin\lua 但已添加到我的 PATH 变量中(我在 Windows 上)

我设置了LUA_PATH环境变量并执行

lua "test\test-suite.lua" -v

test-suite.lua里面的代码就是上面描述的测试代码

【问题讨论】:

  • 你的当前目录是什么?
  • @JosephSible-ReinstateMonica 编辑部分的详细信息
  • @LoïcPetit - 你应该在 Lua 路径中的任何地方使用 ?.lua 而不是 ?
  • @EgorSkriptunoff oO 仅此而已...谢谢!您可以添加您的评论作为答案吗?

标签: lua require


【解决方案1】:

正如@EgorSkriptunoff 建议的那样,在路径中添加文件扩展可以解决问题...

例如:

  • 路径错误 D:\Projets\wow-addon\HeyThere\?
  • 好路径 D:\Projets\wow-addon\HeyThere\?.lua

扩展名应该在路径变量中,因为在 require 中,点被替换并用作文件夹分隔符。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-23
    • 2016-07-10
    • 1970-01-01
    • 2016-02-22
    • 1970-01-01
    相关资源
    最近更新 更多