[英]Run python script from Node.js
我正在尝试从 Node.js 运行一个简单的 python 脚本,我已经安装了python-shell
包,这是我的代码:
var PythonShell = require('python-shell');
var options = {
mode: 'text',
pythonPath: '/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/',
pythonOptions: ['-u'],
scriptPath: '.',
args: ['value1', 'value2', 'value3']
};
PythonShell.run('my_script.py', options, function (err, results) {
if (err) throw err;
// results is an array consisting of messages collected during execution
console.log('results: %j', results);
});
Python:
import sys
arg1 = sys.argv[1] #value1
arg2 = sys.argv[2] #value2
arg3 = sys.argv[3] #value3
print arg1, arg2, arg3
执行node test.js
我有这个错误:
Error: spawn EACCES
at exports._errnoException (util.js:907:11)
at ChildProcess.spawn (internal/child_process.js:298:11)
at exports.spawn (child_process.js:362:9)
at new PythonShell (/Users/Antonio/node_modules/python-shell/index.js:59:25)
at Function.PythonShell.run (/Users/Antonio/node_modules/python-shell/index.js:159:19)
at Object.<anonymous> (/Users/Antonio/Desktop/script/prova.js:11:13)
at Module._compile (module.js:409:26)
at Object.Module._extensions..js (module.js:416:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
我已经使用sudo chmod -R a+rwx my_script.py
设置了 my_script.py 的权限。 我该如何解决这个错误?
要解决该错误,您需要修复包含 Python 路径或位置的字符串。
不像在设置 PATH 中那样使用 PATH 来访问模块、API、库和可能需要的任何其他文件。 而是它所在的驱动器上的常规位置。 “目录映射”或位置(搜索文件夹示例时显示,/usr/bin/python 也可以是 /usr/bin/python2.7)这取决于实际的 .exe python 文件所在的位置。 因此,请给出指示(或者由于缺少更好的词 Path 而不是 PATH ,您设置的像系统路径或任何自定义路径都是错误的)。 可执行文件的简单位置应该在字符串中,所以它也不应该以 / 结尾。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.