Lua Require function on a full path name - path

I need to call the require on a lua file that will not always be in the same place. I was trying to call require on the full path name but that doesn't seem to be working either. I even tried replacing one of my working normal requires with a correct full path name to the same file
example changing
require "foo"
to
require "C:\Users\Me\MyLuaProject\foo"
but when i switched it to the full path name it could no longer find it. So I am wondering if you can even call require on a full path and if not how would i achieve the same result differently?

If you just need to load a file, use dofile, which takes a path:
dofile("C:\\Users\\Me\\MyLuaProject\\foo")

Add the directory containing the file to package.path:
package.path = package.path .. ";C:\\Users\\Me\\MyLuaProject"
require "foo"
You can also add it to the LUA_PATH environment variable, but this is probably less easy to modify on the fly.
A common pattern for modules is to have abc.lua and abc/xyz.lua; to require files in a subdirectory like that, use the following:
require "abc"
require "abc.xyz"

Related

How to find the path of the file calling require?

I have a large test_helper file which includes code for unit and integration test. I would like to split it into two, without having to change every require "test_helper".
To accomplish that, I want to conditionally call unit_test_helper or integration_test_helper based on the file path of the test file calling require "test_helper".
How can I find the path of the file calling require "test_helper"?

How does package and require work?

I have a module named load_modbus.
This is how i require it:
driver_modbus = require "lua_plugin/load_modbus";
Before I require it, I have these code lines:
-- Include Paths
package.path = package.path .. ";./usr/lua/?.lua;./usr/lua/lua_modules/?.lua";
package.cpath = package.cpath .. ";./lib/?.so;./usr/lib/?.so";
How does the require work now?
Does it take the path which I gave to require, (lua_plugin/load_modbus) and place it instead of the ? ?
Am I correct that it will search for these files:
./usr/lua/lua_plugin/load_modbus.lua;
./usr/lua/lua_modules/lua_plugin/load_modbus.lua
./lib/lua_plugin/load_modbus.so;
./usr/lib/lua_plugin/load_modbus.so
It would be nice if someone can tell me if I am correct or not. Still try to understand how it works.
Short answer: Yes.
Require does not assume you give it a path, but a template. This is because lua can be used on systems that don't have an actual file system. If you give it a string like "/include/?.lua" and require "test" it will replace the "?" with the string you required and try to load "/include/test.lua".
You are mostly correct with the paths it will search, but keep in mind that if you do package.path = package.path .. <something> will just be appended to the standard search path, so it will not only search that path, but all the others too. If you only want to search one path, you'd have to do package.path = <your search path>
There is some more information on this at http://lua.org/manual/5.3
Consider reading that. It might also be interesting to you that lua caches the result of it's require calls, in case you didn't know that already.

how to load modules in different folders

I have following folder structure
--folder1
------main.lua
------sub_folder
---------func1.lua
--folder2
------func2.lua
I want to load func1.lua and func2.lua in main.lua by require command
PS:what does the init.lua in a folder do
In vanilla lua, you'd load them like this:
local func1 = loadfile "sub_folder/func1.lua"
local func2 = loadfile "../folder2/func2.lua"
See: Pil Chapter 8
If you're using Lua embedded in some other application (i.e, a game, software, etc.), then you need to let us know what this is, or we can't help you. This goes for your P.S question as well. But generally, init.lua is usually the first file to be executed, i.e, it'll load other files, etc.
The func1.lua is easily loaded via
require 'sub_folder.func1'
because sub_folder is in the same folder as main.lua.
For func2.lua, there is no way to tell require to search "one level up", so you must tell Lua how to find the required modules. This can be done at least two ways:
via LUA_PATH environment variable:
append path/to/folder2/?.lua to it, then main.lua can do require "func2".
OR append path/to/parent-of-folder2/?.lua to it, then main.lua can do require "folder2.func2".
by editing package.path in your script:
package.path = package.path .. ';../?.lua'
require 'func2'
or
package.path = package.path .. ';../../?.lua'
require 'folder2.func2'
The first method is more "permanent" since the setting is in the OS environment; it will work even if you move folder1 to some other place on your system, without moving folder2. The second method is dynamic so it will work regardless of where you place your folder structure, i.e., if folder2 is always sibling folder of folder1 the method 2 works, method 1 fails (or requires that you edit the LUA_PATH).

How to navigate with .lua to a directory above

I have something like this.
/config.lua
/client/init.lua
How can I include the config.lua with include() or dofile()?
Lg
You can (and probably should) do this with require by adding ../?.lua to package.path, like so:
package.path = package.path .. ";../?.lua"
require "config"
See the require and package.path docs for more info.
You're on the right track with dofile. (However, there is no include function in Lua.) As you may have noticed, you can't do this with require:
local cfg = require'../config'
require typically works from the directory of the initial script in a one-way direction, and any required modules which require their own modules must use relative names from that starting point to work properly.
I.e., if you have the following structure:
--+ main.lua requires 'lib.test1'
+-- lib/test1.lua requires 'test2'
+-- lib/test2.lua
test1.lua will fail to require test2 because it cannot be found from the initial directory. lib.test2 is the appropriate module name here. I'm not sure if there are any good patterns for this, short of writing your own stateful require, but it's helpful to know about when writing library code.
Perhaps it's a bad sign when it comes to this.
Going back to the question, you could make an exemption for your config file in package.loaded. This is effectively loading it manually:
package.loaded.config = dofile'../config.lua'
-- ...
local cfg = require'config'

Do file only once in Lua

I was wondering if there's a way to do a lua file only once and have any subsequent attempts to do that lua file will result in a no-op.
I've already thought about doing something akin to C++ header's #if/else/endif trick. I'm wondering if there's a standard way to implement this.
James
well, require pretty much does that.
require "file" -- runs "file.lua"
require "file" -- does not run the "file" again
The only problem with require is that it works on module names, not file names. In particular, require does not handle names with paths (although it does use package.path and package.cpath to locate modules in the file system).
If you want to handle names with paths you can write a simple wrapper to dofile as follows:
do
local cache={}
local olddofile=dofile
function dofile(x)
if cache[x]==nil then
olddofile(x)
cache[x]=true
end
end
end
based on lhf's answer, but utilising package, you can also do this once:
package.preload["something"]=dofile "/path/to/your/file.lua"
and then use:
local x=require "something"
to get the preloaded package again. but that's a bit abusive...

Resources