javascript - 从 Node.js 运行 python 脚本

标签 javascript python 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 路径或位置的字符串。

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

关于javascript - 从 Node.js 运行 python 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39570305/

相关文章:

javascript - 如何隐藏JWPlayer 6 播放按钮?

python - 从包导入后卸载*

python - AppRegistryNotReady : The translation infrastructure cannot be initialized

php - 检查用户是否通过电子邮件激活其帐户并在单击激活链接后显示消息 PHP/JavaScript

javascript - 默认值不是?选择选项 Angularjs 中的对象 :null ?

python - 三角形边和程序

node.js - 尝试在 Heroku 上部署 Node.js/Express/Socket.io 应用程序时出现应用程序错误

node.js - 将对等依赖项与本地依赖项(文件 :. ./some-lib)一起使用

javascript - Redis内部如何分离数据?

javascript - 延迟不起作用