Is there a global variable in Lua that contains the path to the file currently being interpreted? Something like Python's __file__
variable?
I ran a quick for k, v in pairs(_G) do print(k) end
in the interpreter to see if I could find anything. Only the following variables were listed?
string xpcall package tostring print os unpack require getfenv setmetatable next assert tonumber io rawequal collectgarbage getmetatable module rawset math debug pcall table newproxy type coroutine _G select gcinfo pairs rawget loadstring ipairs _VERSION dofile setfenv load error loadfile
Any suggestions on how to get the path the file currently being executed?
In Lua 5.2, when a script is loaded via
require
, it receives as arguments the module name given torequire
and the filename thatrequire
used to open the script:In Lua 5.1, only the module name is passed, not the filename.
In response to the answer by lhf:
Being new to Lua, I was initially confused what
...
meant. Turns out it's a vararg just like with ANSI C: https://www.lua.org/manual/5.3/manual.html#3.4. In my experience with lua 5.3, usinggot me the package name as when used in a require and the absolute file path of the package.
The debug library has a
getinfo
method you can call, which can return, amongst other things, the source file for a function.That would return the name of the source file (which will begin with an @ symbol, indicating it is a filename) of the function at the first level of the call stack. By passing
1
you are asking for information about the current function. If you passed in0
it would return=[C]
as it would be returning information about thegetinfo
function itself.For more detailed information check out the
Programming in Lua
reference on the official Lua website: http://www.lua.org/pil/23.1.html