我正在编写一个Lua脚本,并试图对与主脚本位于同一目录中的文件使用require。我似乎无法得到要求在这种情况下工作,并尝试了几种解决方案,我发现但似乎没有工作。我有以下的文件一起在一个目录:Lua相对路径需要
main.lua
helper.lua
我尝试了以下解决方案,并得到了错误的每个以下:
解决方案1:
local folderOfThisFile = (...):match("(.-)[^%.]+$")
local helper = require(folderOfThisFile .. 'helper')
lua: ...domizerWPF\DataFiles\LUA\main.lua:2: attempt to index local 'pathOfThisFile' (a nil value)
stack traceback:
...domizerWPF\DataFiles\LUA\main.lua:2: in main chunk
[C]: ?
解决方案2:
package.path = "/?.lua;" .. package.path
local helper = require('helper')
lua: ...domizerWPF\DataFiles\LUA\main.lua:2: module 'helper' not found:
no field package.preload['helper']
no file '/helper.lua'
no file '.\helper.lua'
no file 'C:\Program Files (x86)\Lua\5.1\lua\helper.lua'
no file 'C:\Program Files (x86)\Lua\5.1\lua\helper\init.lua'
no file 'C:\Program Files (x86)\Lua\5.1\helper.lua'
no file 'C:\Program Files (x86)\Lua\5.1\helper\init.lua'
no file 'C:\Program Files (x86)\Lua\5.1\lua\helper.luac'
no file '.\helper.dll'
no file '.\helper51.dll'
no file 'C:\Program Files (x86)\Lua\5.1\helper.dll'
no file 'C:\Program Files (x86)\Lua\5.1\helper51.dll'
no file 'C:\Program Files (x86)\Lua\5.1\clibs\helper.dll'
no file 'C:\Program Files (x86)\Lua\5.1\clibs\helper51.dll'
no file 'C:\Program Files (x86)\Lua\5.1\loadall.dll'
no file 'C:\Program Files (x86)\Lua\5.1\clibs\loadall.dll'
stack traceback:
[C]: in function 'require'
...domizerWPF\DataFiles\LUA\main.lua:2: in main chunk
[C]: ?
我已经尝试了解决方案2上的各种路径变体,如“?.lua;”和“./?.lua”无济于事。
过程的当前工作目录是什么? –
你是指lua exe文件还是脚本文件? lua exe文件位于程序文件(x86)中。该脚本位于单独的文件夹中,如下所示:c:\ lua files \。 – Lance
据我所知,'lua.exe' - require会查找与CWD相关的文件。 –