[英]calling python script from node.js
Here is my script : my.py这是我的脚本:my.py
def printme( str ):
# print str;
return str;
printme("I'm first call to user defined function!");
printme("Again second call to the same function");
My node script : testpy.js我的节点脚本:testpy.js
var PythonShell = require('python-shell');
var pyshell = new PythonShell('my.py');
pyshell.on('message', function(message) {
console.log(message);
});
but getting error但得到错误
events.js:85
throw er; // Unhandled 'error' event
Error: spawn python ENOENT
at exports._errnoException (util.js:746:11)
at Process.ChildProcess._handle.onexit (child_process.js:1046:32)
at child_process.js:1137:20
at process._tickCallback (node.js:355:11)
at Function.Module.runMain (module.js:503:11)
at startup (node.js:129:16)
at node.js:814:3
PS I have install Python shell PS我已经安装了Python shell
Also if I want to execute individual function from node.js to python script.另外,如果我想从 node.js 到 python 脚本执行单个函数。 can I do that ?Help
我可以这样做吗?帮助
You can simply write the 'my.py' file like this-您可以像这样简单地编写“my.py”文件-
def printme(str):
return str;
print(printme("I'm first call to user defined function!"));
Check if the path given is correct and check for indentation errors.检查给定的路径是否正确并检查缩进错误。
You can simply import Npm Pythonshell using let keyword instead of const Keyword.您可以简单地使用let关键字而不是const关键字导入 Npm Pythonshell。
let {PythonShell} = require('python-shell')
this works for me这对我有用
Your print statement (my.py line 2) is commented out so nothing will be output and the message event will therefore never fire.您的打印语句(my.py 第 2 行)被注释掉,因此不会输出任何内容,因此消息事件将永远不会触发。 Uncomment your print statement, the Node PythonShell object will redirect the stdout (which print writes to) and fire a message event with the output.
取消注释您的打印语句,Node PythonShell 对象将重定向标准输出(打印写入)并使用输出触发消息事件。
As for your error, it looks like the python script isn't being found in the current directory.至于你的错误,看起来在当前目录中没有找到python脚本。 See https://docs.python.org/2/library/errno.html for error codes and what they mean.
有关错误代码及其含义,请参阅https://docs.python.org/2/library/errno.html 。 Make sure your script is in the right directory or set your python shell to the correct directory using os.chdir.
确保您的脚本位于正确的目录中,或者使用 os.chdir 将您的 python shell 设置为正确的目录。
I think that you need to set up the python script to take in standard input like this import sys for v in sys.argv[1:]: print v
我认为您需要设置 python 脚本以接收标准输入,例如
import sys for v in sys.argv[1:]: print v
Also when setting up the code the way you have it you need to do a PyhtonShell.send('message')
, but I would need to see more of your code because I don't see how you are sending data to the python shell through Node.js.此外,在按照您拥有的方式设置代码时,您需要执行
PyhtonShell.send('message')
,但我需要查看更多代码,因为我看不到您如何将数据发送到 python shell通过 Node.js。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.