我正在编写一个Lua脚本,试图在与主脚本相同的目录中使用require加载一个文件。但是我似乎无法使require在这种情况下工作,并尝试了几种解决方案,但都没有成功。我有以下文件放在同一个目录下:
我已经尝试了以下解决方案,并在每个解决方案后面都遇到了错误: 解决方案1:
解决方案2:
我已经尝试了各种路径如"?.lua;"和"./?.lua;"等对解决方案2进行变化,但都没有成功。
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]: ?
我已经尝试了各种路径如"?.lua;"和"./?.lua;"等对解决方案2进行变化,但都没有成功。
lua.exe
- require 会相对于当前工作目录查找文件,就我所知。 - Alexander Gladysh