Reputation: 611
I am writing a Lua script and am trying to use require on a file that exists in the same directory as the main script. I cannot seem to get require to work in this case and have tried several solutions I have found but none seem to work. I have the following files together in a directory:
main.lua
helper.lua
I've tried the following solutions and gotten the error following each:
Solution 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]: ?
Solution 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]: ?
I've tried variations on Solution 2 with various paths such as "?.lua;" and "./?.lua;" to no avail.
Upvotes: 5
Views: 4748
Reputation: 3115
If you mean that you want to be able to invoke the program from any directory and that it correctly locates the require
d files, then you can use this solution (you only need it within main.lua
):
local base_path = string.match(arg[0], '^(.-)[^/\\]*$')
package.path = string.format("%s;%s?.lua", package.path, base_path)
This works by adding the directory where the file is to the package path, so that require
can work on the files in that directory. Lua doesn't do this automatically yet (Python does, since version 2.6 or so); hopefully it will be implemented in future. You can also use base_path
to refer to other files in the same directory. In my case, for example, there is a SQLite database in that directory and the program needs to open it, so I use this:
local database_filename = base_path .. 'db.sqlite'
You can also make base_path
a global so that it's available to other modules if necessary.
Upvotes: 0
Reputation: 53
i am still learning lua but here is what I can whip up for you, if you don't have a file system API installed then you can make a string variable with your curent working dir in it and you can add to it like this
local cwd="C:\users\user\Desktop\"
dofile(cwd.."program.lua")
thats what I do, and I have no problems with it
Upvotes: 0
Reputation: 69934
These two lines of the error message shed some light on your problem:
no file '/helper.lua'
no file '.\helper.lua'
The first line is due to your change to package.path. As you can see, it looks for a "/helper.lua" file that doesn't exist so its not doing anything. The second line is due to the default package.path and is looking for a "helper.lua" in the current working directory. Since its not finding, your current working directory must not be the directory your main.lua is located on.
The fix is to either make the current working directory the directory where main.lua and helper.lua are located or to add "C:\\path\\to\\your\\lua\\project\\?.lua"
to the package.path
Upvotes: 2