在 Nodejs 中使用 PythonShell 模块 [英] Using PythonShell module in Nodejs

查看:172
本文介绍了在 Nodejs 中使用 PythonShell 模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试遵循 npm pythonshell 此处的文档.我只是想得到一个简单的例子来正常运行但没有成功,网上的例子并不多,但我确实尝试遵循这个 stackoverflow 上的一个.那么我的代码到底有什么问题呢?我不确定为什么会收到以下错误.我在 python 中更舒服,nodejs 对我来说是新领域.感谢您的帮助.

I have been trying to follow the documentation for the npm pythonshell here. I'm just trying to get a simple example to function correctly with no success, not many examples online but I did try to follow this one on stackoverflow. So what exactly is wrong with my code here? I'm not really sure why I'm getting the below error. I'm far more comfortable in python, nodejs is new territory for me. Thank you for any help.

test.py 代码:

print("This is a test")

Nodejs 与 pythonshell 测试:

var PythonShell = require('python-shell');

    var options = {
      mode: 'text',
      pythonPath: '/bin/bash/python2', 
      pythonOptions: ['-u'],
      scriptPath: './TestProject/',
      args: ['value1', 'value2', 'value3']
    };

    PythonShell.run('test.py', options, function (err, results) {
      if (err) throw err;
      // results is an array consisting of messages collected during execution
      console.log('results: %j', results);
    });

这是我的错误

    internal/child_process.js:313
    throw errnoException(err, 'spawn');
    ^

Error: spawn ENOTDIR
    at exports._errnoException (util.js:1022:11)
    at ChildProcess.spawn (internal/child_process.js:313:11)
    at exports.spawn (child_process.js:387:9)
    at new PythonShell (/home/nomad/TestProject/node_modules/python-shell/index.js:59:25)
    at Function.PythonShell.run (/home/nomad/TestProject/node_modules/python-shell/index.js:159:19)
    at Object.<anonymous> (/home/nomad/TestProject/app.js:11:13)
    at Module._compile (module.js:571:32)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:488:32)
    at tryModuleLoad (module.js:447:12)

推荐答案

所以我犯了一个错误,没有在新系统安装中检查我的 python 路径,但是使用绝对路径也是脚本正常运行的必要条件.这是任何尝试使用 nodejs python-shell npm 运行 python 脚本的人的工作示例.感谢 Bryan 帮助我解决 javascript 错误.

So I made the mistake of not checking my python path on a new system install, however using an absolute path is also nessasary for the script to function. Here is a working example for anyone trying to run python scripts using nodejs python-shell npm. Thanks to Bryan for helping me with javascript error.

var PythonShell = require('python-shell');

    var options = {
      mode: 'text',
      pythonPath: '/usr/bin/python', 
      pythonOptions: ['-u'],
      // make sure you use an absolute path for scriptPath
      scriptPath: '/home/username/Test_Project/Python_Script_dir',
      args: ['value1', 'value2', 'value3']
    };

    PythonShell.run('test.py', options, function (err, results) {
      if (err) throw err;
      // results is an array consisting of messages collected during execution
      console.log('results: %j', results);
    });

这篇关于在 Nodejs 中使用 PythonShell 模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆