
前提・実現したいこと
Node.jsとPythonを連携させたシステムを作る。
pythonshellというライブラリを使って、Node.jsからpythonを実行しようとしています
そして、python上で求めた変数をNode.js上で表示させたいと思っています
発生している問題・エラーメッセージ
参考サイトにあるサンプルは動作させることが出来たのですが、自分の実行させたいファイルを実行することが出来ません。
そのプログラムはargparseを用いてテキストファイルを用いて入出力しているのですが、実行のところで詰まっています。
python-shell
参考にしたサイト
エラーメッセージ
TypeError: self.stderrParser is not a function at /Users/admin/Desktop/ws-v/node_js/node_modules/python-shell/index.js:302:42 at Array.forEach (<anonymous>) at PythonShell.recieveInternal (/Users/admin/Desktop/ws-v/node_js/node_modules/python-shell/index.js:298:15) at PythonShell.receiveStderr (/Users/admin/Desktop/ws-v/node_js/node_modules/python-shell/index.js:282:21) at Socket.<anonymous> (/Users/admin/Desktop/ws-v/node_js/node_modules/python-shell/index.js:100:18) at emitOne (events.js:116:13) at Socket.emit (events.js:211:7) at addChunk (_stream_readable.js:263:12) at readableAddChunk (_stream_readable.js:250:11) at Socket.Readable.push (_stream_readable.js:208:10)
Node.js
var {PythonShell} = require('python-shell'); var shell = new PythonShell('pysawit.py',{ mode: 'run', scriptPath: './', args: ['-i',"/Users/admin/Desktop/ws-v/python/examples/ini.txt", '-o',"/Users/admin/Desktop/ws-v/python/examples/ini.txt",'-n',22] }); // facade.pyからの返事を待機 shell.on('message', data => { // データを表示 console.log(data.result); }); // 入力を終了 shell.end();
Python
def output_dailyrun(self, initialize=False): """ Prepare the list of daily model output, then retrieve and store their results. # Arguments initialize (bool): `True` to initialize the output dictionry, else `False` to retrieve and store model output results in dictionary # Returns None: """ out = self.out ops = self.model if not initialize: # 1. get results for daily properties: ag_growth = ops.parts.pinnae.growth + ops.parts.rachis.growth + ops.parts.trunk.growth veg_growth = ag_growth + ops.parts.roots.growth out['age'] = ops.treeage out['tmin'] = ops.daytmin out['tmax'] = ops.daytmax out['totalrad'], out['directrad'], out['diffuserad'] = ops.dayrad out['wind'] = ops.daywind out['rain'] = ops.dayrain out['netrain'] = ops.netrain out['ambientCO2'] = ops.co2ambient out['LAI'] = ops.lai out['pinnae'] = ops.parts.pinnae.weight out['rachis'] = ops.parts.rachis.weight out['trunk'] = ops.parts.trunk.weight out['roots'] = ops.parts.roots.weight out['male'] = ops.parts.maleflo.weight out['female'] = ops.parts.femaflo.weight out['bunches'] = ops.parts.bunches.weight out['flowersex'] = ops.flowersex out['VDM'] = ops.vdmwgt out['TDM'] = ops.tdmwgt out['assim_photosyn'] = ops.dayassim out['assim_maint'] = ops.assim4maint out['assim_growth'] = ops.assim4growth out['assim_gen'] = ops.assim4gen out['VDM_growth'] = ag_growth out['TDM_growth'] = veg_growth out['yield'] = ops.bunchyield out['trunk_hgt'] = ops.trunkhgt out['rootdepth'] = ops.rootdepth out['rootzone_VWC'] = ops.rootwater.vwc # out['rootzone_pF'] = ops.rootwater.pF out['waterstress'] = ops.waterstresses.crop out['actual_E'] = ops.aet.soil out['actual_T'] = ops.aet.crop out['pot_T'] = ops.dayet.crop # out['runoff'] = ops.runoff # add soil layers output to the list # オブジェクトを作成 response = {"result": ops.rootwater.vwc} # JSON文字列にして出力 print(json.dumps(response))
pythonのプログラムは長いので省略しています。
###試したこと
Node.jsのファイルとpythonのファイルは同じ階層にあって
python pysawit.py run -i "/Users/admin/Desktop/ws-v/node_js/python/examples/ini.txt" -o "/Users/admin/Desktop/ws-v/node_js/python/examples/output.txt" -n 22 で実行できます。
要するに、上のスクリプトをNode.jsで実行してpythonで求めた変数をNode.jsに送信して表示させたいのです。
情報が少なくて大変困っています。
下手な質問で申し訳ありませんが、どうか、ご指摘の方よろしくお願いします。
もう少し簡単なやり方があれば教えていただけるとありがたいです。
回答1件
あなたの回答
tips
プレビュー