【发布时间】:2016-09-19 09:57:33
【问题描述】:
我正在尝试从 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 设置了权限。
我该如何解决这个错误?
【问题讨论】:
-
pythonPath应该指向python可执行文件,而不是路径(因为字符串末尾有一个'/')。 -
@acw1668 那么pythonPath应该是什么?
-
这取决于 Python 可执行文件的位置。例如,
/usr/bin/python。 -
/usr/bin/python 用这个没关系
标签: javascript python node.js