TorbenJ
TorbenJ

Reputation: 4582

Lua import library from local path (luasocket)

I'm quite new to Lua and I have some problems with the import mechanism.
From other languages I'm used to do something like this:

include "./lib/mylib.h"

or in general just to pass the local path to the file I would like to include.

Now in Lua it is a bit confusing to me. I read something about that require looks for the library in the lua path.
In my case I would like to create a script that uses the luasocket library but I would like to import it from a local path so that I can deploy it without the need to have it on another machine in the lua path.

From other forums I got solutions like creating a local require function that looks in a given local library folder but that didn't work.

Then I read that I can just type require 'myfile' to import a different file locally but luasocket requires a core.dll and if I just move the lua files of the library to my local path it can't find the dll.

Is there an easy way to solve this. What do I have to do to get this working?

Upvotes: 1

Views: 2370

Answers (1)

Oliver
Oliver

Reputation: 29493

It is sufficient for the .dll extension to be in the LUA_CPATH (so it ends up in package.cpath). For example, put the socket.lua in C:\Foo, and put core.dll in same folder. Then require 'socket' will fail:

> print(package.cpath)
.\?.dll;.\?51.dll;C:\Program Files\Lua\5.1\?.dll;C:\Program Files\Lua\5.1\?51.dl
l;C:\Program Files\Lua\5.1\clibs\?.dll;C:\Program Files\Lua\5.1\clibs\?51.dll;C:
\Program Files\Lua\5.1\loadall.dll;C:\Program Files\Lua\5.1\clibs\loadall.dll
> print(package.path)
;.\?.lua;C:\Program Files\Lua\5.1\lua\?.lua;C:\Program Files\Lua\5.1\lua\?\init.
lua;C:\Program Files\Lua\5.1\?.lua;C:\Program Files\Lua\5.1\?\init.lua;C:\Progra
m Files\Lua\5.1\lua\?.luac
> require 'socket'
.\socket.lua:13: module 'socket.core' not found:
        -- look for preload
        no field package.preload['socket.core']
        -- look for .lua in socket folder anywhere on package.path:
        no file '.\socket\core.lua'
        no file 'C:\Program Files\Lua\5.1\lua\socket\core.lua'
        no file 'C:\Program Files\Lua\5.1\lua\socket\core\init.lua'
        ...
        -- look for .luac in socket folder anywhere on package.path:
        no file 'C:\Program Files\Lua\5.1\lua\socket\core.luac'
        -- look for .dll in socket folder anywhere on package.cpath:
        no file '.\socket\core.dll'
        no file '.\socket\core51.dll'
        no file 'C:\Program Files\Lua\5.1\socket\core.dll'
        no file 'C:\Program Files\Lua\5.1\socket\core51.dll'
        no file 'C:\Program Files\Lua\5.1\clibs\socket\core.dll'
        no file 'C:\Program Files\Lua\5.1\clibs\socket\core51.dll'
        ...
stack traceback:
        [C]: in function 'require'
        .\socket.lua:13: in main chunk
        [C]: in function 'require'
        stdin:1: in main chunk
        [C]: ?

because socket.lua requires socket.core, so Lua interpreter looks for core.lua in a "socket" folder anywhere on package.path, doesn't find it (socket folder doesnt' exist), then looks for core.dll in socket folder anywhere on package.cpath, same problem.

Now create C:\Foo\socket folder, and move core.dll to it, then require 'socket' will work (from interpreter started while cd to C:\Foo).

In your case, if you have yourLuaScript.lua in C:\Foo, you could have socket.lua in same folder, core.dll in C:\Foo\socket, and yourLuaScript.lua could require 'socket' and, as long as package.path contains .\*.lua, and package.cpath contains .\*.dll, you will be able to run yourLuaScript.lua.

Upvotes: 2

Related Questions