Timothy Salazar
Timothy Salazar

Reputation: 11

Unable to require in Lua

My understanding of require is as follows: If the file you are requiring is either in the same directory that you are requiring from, or is in package.path, you will be able to require it.

For me however, I get a Module not found! error, even when the files are in the same directory.

As for package.path, the directories listed do not exist on my PC. Here is my package.path:

/usr/local/share/lua/5.2/?.lua;/usr/local/share/lua/5.2/?/init.lua;/usr/local/lib/lua/5.2/?.lua;/usr/local/lib/lua/5.2/?/init.lua;/usr/share/lua/5.2/?.lua;/usr/share/lua/5.2/?/init.lua;./?.lua

There are no folders named 'lua' on my PC (that I can find) other than ones created by me for different purposes. This leads me to suspect a bad install, although I did try to uninstall Lua and got Virtual packages like 'lua' can't be removed. This could be relevant, I'm not sure. But other than the inability to require modules and the lack of directories named 'lua', everything works fine.

At this point I'm not sure what to do. I can't find much information on the subject other than "append to your package.path!" but I would rather be able to simply have the two aforementioned files in the same directory for this to work.

How can I fix this?

I also have small question: Are there default modules/packages that come with lua?

Edit:
OS - Ubuntu Linux
Lua version - 5.2
I am running foo = require('bar')
I am not trying to require a binary module, I am trying to require a lua file that I have written.

Upvotes: 0

Views: 6738

Answers (4)

njamescouk
njamescouk

Reputation: 179

use dofile("path/to/file.lua")

Upvotes: 0

koyaanisqatsi
koyaanisqatsi

Reputation: 2823

Your understanding is as poor as mine when i starting to require() files.
Now i understand that ( Lua for Linux )...

  1. require() first looks in package.loaded
  2. If not there it looks for a loader function in: package.preload
  3. If 1. and 2. fail then it will try the search paths for: *.lua (Lua Code) *.bin (Lua bytecode) and *.so (Library)

My recommendation for require() is to write a package.preload function that returns what you want in package.loaded.
Because you can do a a loadfile() or dofile() with the full or relative path in there.
That dont need modifications of the package.path.
And therefore it is also possible to start the main script without change directory to the main script location.

Example for a package.preload function...

-- A loader for require('test')
package.preload.test=function() return {test='test'} end
-- Now require it...
newtable=require('test')
-- Using it...
print(newtable.test) -- Puts out: test

Example preload function with loadfile()

-- Add a loader for require('rot')
package.preload.rot=loadfile('/home/knoppix/lua/rot.lua')

Your second question is selfanswering if you look in: package.loaded
As i wrote require() looks first in there.
Try for that...

_G=empty -- Deleting _G
-- Now reinstall it simply by...
_G=require('_G')
-- It will be restored from: package.loaded

Upvotes: 1

Promitheas Nikou
Promitheas Nikou

Reputation: 521

You can try reading the file as a string, and then compiling and executing the string. For example:

--Instead of writing "require 'mymodule'" you can say:


--Read the file contents
local f=io.open("mymodule.lua","r")
local s = f:read("*a")

--loadstring compiles a chunk of code and returns a function. On some Lua versions though, loadstring isn't defined, and so, you can use load (same thing, different name)

loadstring = loadstring or load

loadstring(s)()

--Close the file object
f:close()

You can turn that into a function:

loadstring = loadstring or load

function myrequire(modulename)
    f = io.open(modulename,"r")
    loadstring(f:read("*a"))()
    f:close()
end

Actually, a similar thing exists, called "dofile". It has the exact same effect as "myrequire".

NOTE: These things will probably do your job, but you should know, that require is not exactly the same as dofile. The difference is that require doesn't only look in the current directory, but also in the directories specified by the "package.path" variable. Also, require stores all loaded modules in "package.loaded", so that it's faster when you try to load the same module many times.

Upvotes: 0

Paul Kulchenko
Paul Kulchenko

Reputation: 26794

You don't provide enough information to help you, as you don't specify the version of Lua you're using (I guess 5.2 judging by the file path), the OS (Linux?), the actual require command you're executing or the location of the lua file you're requiring relative to the location of the file you're requiring it from, and so on.

Your assumptions are correct, but there may be multiple issues that would prevent you from loading the file. For example, it may be in the correct location, but instead of require "foo" you are using require "foo.lua", which is not going to work. Or you are trying to load a binary module (which is not going to be affected by package.path configuration). Or there is a dependency from the module you're loading that is not satisfied.

Are there default modules/packages that come with lua?

Yes, like debug, math, io, and others, but you won't see them as Lua files, as they are compiled as C modules together with the rest of the Lua interpreter.

Upvotes: 1

Related Questions