【问题标题】:Run python script from Node.js从 Node.js 运行 python 脚本
【发布时间】: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


【解决方案1】:

要解决错误,您需要修复包含 Python 的路径或位置的字符串。

不是设置 PATH 中的 PATH 来访问模块、API、库和可能需要的任何其他文件。而是它所在的驱动器上的常规位置。 “目录映射”或位置(在搜索文件夹示例时显示,/usr/bin/python 也可以是 /usr/bin/python2.7)这取决于实际 .exe python 文件的位置。 因此,请给出指示(或者因为缺少更好的词路径而不是您设置的路径,例如系统路径或任何自定义路径都是错误的)。可执行文件的简单位置应该在字符串中,因此也不应该以 / 结尾。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-02-18
    • 1970-01-01
    • 2014-05-04
    • 2019-06-11
    • 2022-07-31
    • 1970-01-01
    • 2020-10-08
    • 2023-04-07
    相关资源
    最近更新 更多