lua模块未加载库 [英] lua module is not loading libraries

查看:97
本文介绍了lua模块未加载库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚接触lua,并且试图了解模块的工作原理.但是我试图将预先存在的模块加载到新脚本中,并从命令行运行该脚本.

I'm just new to lua and I'm trying to understand how modules work. But I'm trying to load a pre-existing module into a new script and run this script from the command line.

我有一个名为main.lua模块的文件,看起来像这样:

I have a file called main.lua module that looks something like this:

module (..., package.seeall)
-- Load libraries
require("luasql.postgres")
require("luasql.sqlite3")

local connect_to_db = function()
   if not global_con then
      env = assert (luasql.postgres())
      global_con = assert (env:connect(databasename, databaseUser, databasepassword, databaseserver))   
      return true
   else
      return false 
   end
end

update_widget = function (parm1, parm2, parm3)
  local connected = connect_to_db()         
  if connected then
     -- do something else
     return true
  end
end -- end function.

我现在正在尝试为此模块创建测试脚本.在单独的lua文件中,我具有以下逻辑:

I'm now trying to create a test script for this module. I have the following logic in a separate lua file:

package.path = '/usr/share/myapp/main.lua;'
local my_object = require("main")

print my_object.update_widget

问题:

尝试运行测试脚本时出现以下错误:

Problem:

I'm getting the following error when I try to run my test script:

attempt to call field 'postgres' (a table value)

它失败的行在connect_to_db()方法中,我尝试在其中创建环境变量:

The line it's failing on is in the connect_to_db() method where I try to create an environment variable:

env = assert (luasql.postgres())

到目前为止我尝试过的事情:

  1. 我已经在测试脚本中修改了package.path以匹配main.lua正在使用的内容.我这样做是通过以Web应用程序驱动的常规"方式执行main.lua,然后将package.path的内容转储到日志文件中的. 我已经从日志文件中复制了路径,并将其用作测试脚本中的package.path值...当然,我必须通过添加其他条目(通向main.lua的路径)来对其进行修改. > 但是除此之外,包路径是相同的.

  1. I've modified the package.path in my test script to match what is being used by main.lua. I did so by executing main.lua the "regular" way - driven by a web app - and dumping out the contents of package.path to a log file. I've copied the path from the log file and used it as the package.path value in my test script... of course, I had to modify it by adding an additional entry - a path leading to main.lua.
    But other than that, the package paths are the same.

我已经在main.lua中添加了打印语句,以证明它正在进入update_widget方法...并且只是尝试创建postgres而失败.

I've added print statements inside main.lua to prove that it is getting into the update_widget method... and that it's just failing trying to create the postgres.

我已经在测试脚本中添加了luasql.postgres库,以查看是否有帮助……像这样:

I've added the luasql.postgres library in the test script to see if that would help... like so:

package.path = '/var/x/appname/main.lua;'

local pgdb = require("luasql.posgres")

print(pgdb)
myenv = assert(lua.postgres()) -- fails

测试脚本也死于试图创建该对象的过程……我将继续寻找.路径一定有问题...但是我看不到Web应用程序加载时创建的路径与测试脚本中的路径之间的区别. 我现在将使用DIFF工具进行比较.

The test script also dies trying to create this object... I'm going to keep hunting around. It must be a problem with the paths... but I can't see the difference between the path thats created when loaded by the web app, vs. what I have in the test script. I'm going to use a DIFF tool to compare for now.

任何建议将不胜感激.

Any suggestions would be appreciated.

谢谢.

编辑1

我绝对认为这是一条路,尽管我现在还看不到这里出了什么问题. 我创建了另一个测试脚本(我们称之为test3).但是这次,我没有通过将值分配给package.path来显式设置路径. 我只是尝试包括luasql.postgres pacakge并以原始测试脚本的方式使用它...并且它起作用了! 所以这是有效的代码:

I definitely think it's the path, although I can't see just yet what's wrong with here. I created yet another test script (let's call it test3).. but this time, I didn't explicitly set the path by assigning values to package.path. I just tried to include the luasql.postgres pacakge and use it the way the original test script does... and it works! So here's code that works:

luasql = require "luasql.postgres"
local myenv = assert (luasql.postgres())
print(myenv)

但这失败了:

package.path = package.path .. ';/usr/share/myapp/main.lua'

luasql = require "luasql.postgres"
myenv = assert (luasql.postgres())
print(myenv)

就大狼人而言,我在lua的交互模式下尝试过...而且我的代码工作得很好.

Also to greatwolf's point, I tried from interactive mode in lua... and my code works just fine.

Lua 5.1.5  Copyright (C) 1994-2012 Lua.org, PUC-Rio
> pgdb = require("luasql.postgres")
> print(pgdb)
table: 0x176cb228
> myenv=assert(luasql.postgres())
> print(myenv)
PostgreSQL environment (0x176c9d5c)
> 

所以...这是交互模式下的package.path变量:

So... here's the package.path variable from interactive mode:

> print(package.path)
./?.lua;/usr/local/share/lua/5.1/?.lua;/usr/local/share/lua/5.1/?/init.lua;/usr/local/lib/lua/5.1/?.lua;/usr/local/lib/lua/5.1/?/init.lua;/usr/share/lua/5.1/?.lua;/usr/share/lua/5.1/?/init.lua
> 

这是我原始测试脚本失败的路径.

And here's the path from my original test script where it fails.

/usr/share/myapp/main.lua; ./?. lua;/usr/local/share/lua/5.1/?. lua;/usr/local/share/lua/5.1/?/init. lua;/usr/local/lib/lua/5.1/?. lua;/usr/local/lib/lua/5.1/?/init.lua;/usr/share/lua/5.1/?. lua;/usr/share/lua/5.1/?/init.lua

/usr/share/myapp/main.lua;./?.lua;/usr/local/share/lua/5.1/?.lua;/usr/local/share/lua/5.1/?/init.lua;/usr/local/lib/lua/5.1/?.lua;/usr/local/lib/lua/5.1/?/init.lua;/usr/share/lua/5.1/?.lua;/usr/share/lua/5.1/?/init.lua

推荐答案

路径存在问题.我仍然不确定到底出了什么问题,但是我将测试脚本中的逻辑从以下位置更改了:

It was a problem with the path. I'm still not sure exactly what was wrong, but I changed my logic in the test script from:

package.path = '/usr/share/myapp/main.lua;' -- resetting package path manually
package.path=package.path ..'./?.lua;/usr/local/share/lua/5.1/?.lua;/usr/local/share/lua/5.1/?/init.lua;/usr/local/lib/lua/5.1/?.lua;/usr/local/lib/lua/5.1/?/init.lua;/usr/share/lua/5.1/?.lua;/usr/share/lua/5.1/?/init.lua

'

package.path = package.path .. '/usr/share/myapp/main.lua' -- append to default path.

现在它找到了lua postgres软件包,也让我调用了函数

Now it finds the lua postgres package and lets me call the functions too

这篇关于lua模块未加载库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆