Why lua require won't search current directory? - lua

say I've two files test.lua and m.lua in a folder, in test.lua as:
require("m")
then I run this file, howerver it raise an error:
lua: /Users/xx/works/scripts/test.lua:43: module 'm' not found:
no field package.preload['m']
no file '/usr/local/share/lua/5.3/m.lua'
no file '/usr/local/share/lua/5.3/m/init.lua'
no file '/usr/local/lib/lua/5.3/m.lua'
no file '/usr/local/lib/lua/5.3/m/init.lua'
no file './m.lua'
no file './m/init.lua'
no file '/usr/local/lib/lua/5.3/m.so'
no file '/usr/local/lib/lua/5.3/loadall.so'
no file './m.so'
As you can see the line no file './m.lua' appears but this is not true. ./m.lua exists, and file permission stuff is OK. If I hardcode the path:
package.path = package.path..';'..'/Users/xx/works/scripts/?.lua'
require('m')
It will work as expected.
What should I do to make lua search current directory fisrt (like python's import) when require a module

The current directory is the directory where you launch lua from.
The command line is missing in your example, if you used lua test.lua then it shoud work, if you used lua works/scripts/test.lua then it will not work.

Related

Managing Lunar Vim (lvim) config.lua by separating it in different files

Trying to write config.lua for lvim that wiil be separated in different files? that will be included in config.lua with require('<package>'). Everything works if i try i in .config/lvim/ directory, but i get below message, when i run lvim in different directory.
21:43:43 [WARN ] lvim: "Invalid configuration: /home/axr/.config/lvim/config.lua:6: module 'base/search' not found:\n\t
no field package.preload['base/search']\n\tno file './base/search.lua'\n\tno file '/usr/share/luajit-2.1.0-beta3/base/s
earch.lua'\n\tno file '/usr/local/share/lua/5.1/base/search.lua'\n\tno file '/usr/local/share/lua/5.1/base/search/init.
lua'\n\tno file '/usr/share/lua/5.1/base/search.lua'\n\tno file '/usr/share/lua/5.1/base/search/init.lua'\n\tno file '.
/base/search.so'\n\tno file '/usr/local/lib/lua/5.1/base/search.so'\n\tno file '/usr/lib/lua/5.1/base/search.so'\n\tno
file '/usr/local/lib/lua/5.1/loadall.so'" file="init.lua", line=49
Tried to replace / with ., nothing changed.
Checked runtimepath, .config/lvim/ was there.
Tried to replace relative path in require(<path>) with full path.
GitHub repository with files and comments: https://github.com/SATANalexander666/lvim-config
Dont use / or \\ in require()
Only use the . for entering a folder.
Using nvim the .config/nvim/lua folder has to be created manually.
After that it is easy doing to require Lua files.
Example
.config/nvim/init.vim # file
.config/nvim/lua/config.lua # file
.config/nvim/lua/base # folder
.config/nvim/lua/keys # folder
.config/nvim/lua/plugins/core # folder
.config/nvim/lua/plugins/packer # folder
Content of init.vim
lua require("config")
Will be appended/finished to: lua/config.lua
Refer nvim' help: :help lua-package-path
Content of config.lua
-- base
require('base.search') -- Search configs
require('base.indents') -- Indentation configs
require('base.visual') -- GUI configs
require('base.other')
-- keys
require('keys.alias') -- Shortcuts and incapsulation
require('keys.main') -- Keys for built-in features
require('keys.plugins') -- Keys for plugged features
-- plugins
require('plugins.core.use') -- Buil-in plugins that are being used
require('plugins.core.config') -- Configs for built-in plugins
require('plugins.packer.use') -- Packer pluggins that are being used
require('plugins.packer.config') -- Configs for packer plugins
The dot will be used to enter the folder(s) (Linux & Windows)
Refer nvim' help: :help lua-require
In /.config/lvim must be created folder named /lua and all folders required in main config.lua must be moved to this folder, however path to required folders shouldn't be changed. Example: require('base.search'), while actual path is /.config/lvim/lua/base/search.lua.

How To Require Lua Socket?

I'm very new to lua development with file manipulation, and now trying to import the lua socket package into my project according to this post, but I can't run even the code below.
I guess the error message indicates I need to import not only the socket.lua but also .\socket\core (probably .dll, since it doesn't have core.lua), while a reply at the post suggested importing only the file.
I'm stuck in just the beginning... What do I have to do for the next step?
local function main()
local socket = require("socket")
end
main()
Exception in thread "main" com.naef.jnlua.LuaRuntimeException: ...n32.win32.x86_64\workspace\TestForCiv\src\socket.lua:13: module 'socket.core' not found:
no field package.preload['socket.core']
no file '.\socket\core.lua'
no file 'C:\Program Files\Java\jre1.8.0_151\bin\lua\socket\core.lua'
no file 'C:\Program Files\Java\jre1.8.0_151\bin\lua\socket\core\init.lua'
...(a bunch of no file errors continues)
Edit: I added the folder structure. Even I add the .dll file it returns the same error.
I don't know the details of you configuration, but try this
require ("src.socket")
you should require a module from the root path of the lib

How to read a data file at a package path on Lua 5.1

How to read a data file at a package path on Lua 5.1?
What I'm looking for is something like a io.read but at the package directory instead of the working directory (arg[0]), and without using hardcoded absolute paths. That would be something like dofile does, but without running the code, only reading it as a string.
Example:
I have a test folder, the current working directory of the test.lua script.
There's a package luapackage in another folder, somewhere specified at the LUA_PATHenviroment variable.
luapackage can:
require("luapackage.other_module");
dofile("other_module.lua").
But luapackage can't do:
io.read("data.txt")
io.read("luapackage/data.txt")
Sample structure:
+-test/
|
+-test.lua
+-luamodule/
|
+-data.txt
|
+-luamodule.lua
|
+-other_module.lua
For this example, test.lua only requires luamodule:
-- test.lua
local luamodule = require("luamodule")
And luamodule needs to read its modules and data files:
-- luamodule.lua
local other_module= dofile("other_module.lua") -- works
-- local other_module= require("luamodule.other_module") -- also works
local data = io.open("data.txt") -- fails
-- local data = io.open("luamodule/data.txt") -- also fails
It doesn't work because it searches for the file at the working directory (test) and not the package directory.
If I place a copy of the package at running script's folder, io.read(luapackage/data.txt) is possible. But every script would have to carry its own local copy of luapackage.
Note: I'm looking for a Lua solution, avoiding binary packages that could compromise cross-compatibility.
You can use debug.getinfo(1,"S").source to get the location of the current (module) file. Replace luamodule.lua with data.txt, remove leading #, and you should get the path you need.

Torch lua requires not finding the files

My directory structure looks like this:
--trian.lua
--util/
-- misc.lua
-- io.lua
-- init.lua
In my train.lua I am using require to include the files in util directory as follows:
require 'util.misc'
require 'util.io'
require 'util.init'
When I execute train.lua, misc and io are loaded but init is not getting loaded. I am not sure how to debug this issue. Could some please help me with the issue?

Using tdbloader.bat to upload a RDF file and querying using tdbquery.bat

I want to upload an RDF file using tdbloader.bat (C:\apache-jena-2.10.0\bat, in my case).
My turtle file is located in C:\apache-jena-2.10.0, and is named test.ttl. What will be the command line to load the turtle file using tdbloader.bat? After it is loaded, what would be a command line to query with tdbquery.bat?
I have used the following command-
tdbloader.bat -loc c:\apache-jena-2.10.0 test.ttl
But it returned a FileNotFound Exception.
The file test.ttl isnt in the same directory as bat files which is causing FileNotFound exception. A correction would be to copy test.ttl to C:\apache-jena-2.10.0\bat. After doing that execute command
tdbloader.bat -loc potato test.ttl.
This would load test.ttl to a folder named potato in same directory as tdbloader.bat. Hope it helps

Resources