Lua在后台执行python脚本

2024-04-27 02:49:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我在后台运行脚本时遇到问题。在

我有Lua文件:

function on_msg_receive (msg)
  if (msg.text=="Alarmon") then
    send_msg (msg.from.print_name, 'Sensor ON!', ok_cb, false)
    io.popen('/home/pi/led.py')
  end
end

Python文件(用于测试):

^{pr2}$

执行后我得到:

/home/pi/led.py: 1: /home/pi/led.py: import: not found
/home/pi/led.py: 2: /home/pi/led.py: import: not found
/home/pi/led.py: 4: /home/pi/led.py: Syntax error: word unexpected (expecting ")")

它的Lua文件用于电报。我运行消息“Alarmon”,然后它工作直到错误弹出。 我认为在python中io.popen不运行led.py的问题。 如何改变?在


Tags: 文件pyioimporthomeledpinot
1条回答
网友
1楼 · 发布于 2024-04-27 02:49:57

在你的代码中你只提供io.popen公司Python脚本的路径。在

function on_msg_receive (msg)
  if (msg.text=="Alarmon") then
    send_msg (msg.from.print_name, 'Sensor ON!', ok_cb, false)
    io.popen('/home/pi/led.py')
  end

你的电脑不知道该怎么处理它。在

Lua 5.3 Reference Manual

io.popen (prog [, mode])

This function is system dependent and is not available on all platforms.

Starts program prog in a separated process and returns a file handle that you can use to read data from this program (if mode is "r", the default) or to write data to this program (if mode is "w").

所以为了让它工作,你必须实际启动一个程序,在你的例子中是Python解释器。 假设在PATH系统变量中有Python解释器的位置,那么应该可以:

^{pr2}$

当您希望interpeter运行脚本时,还需要提供脚本的路径作为参数。在

io.popen('Python /home/pi/led.py')

就像你进入命令行界面一样。。。在

如果您不打算使用程序的任何输入或输出,您可以简单地使用:

os.execute('Python /home/pi/led.py')

取而代之的是

相关问题 更多 >