Reputation: 59
I have a lua script A.lua, which calls B.lua by specifying package.path. In B.lua, it has to use its absolute path. My question is, how can I get the absolute path of B.lua in B.lua? "PWD" doesn't work since PWD just return the current working directory. debug doesn't work either, since B.lua is not call from command line. Anyone have better solution?
Upvotes: 2
Views: 5096
Reputation: 4264
If you have a file somewhere in package.path
that require
is able to find, then you can also easily get the path by using package.searchpath
.
If "foo.bar.baz"
is the name under which require
will load the file, then
package.searchpath( "foo.bar.baz", package.path )
--> (e.g.) "/usr/share/lua/5.3/foo/bar/baz.lua"
gets you the path.
This works in Lua 5.2 or newer and LuaJIT as well.
Upvotes: 3