質問するログイン新規登録

回答編集履歴

1

コメントしたものと同じものを貼りました。

2019/07/03 07:52

投稿

suama
suama

スコア1997

answer CHANGED
@@ -24,4 +24,46 @@
24
24
  - 文字コードがSJISかどうかとか、日本語が混じってないか
25
25
  - 直接 python test.py として実行した際はエラーがないか
26
26
 
27
- もし、日本語を含むソースコードになっていたら、``# -*- coding: utf-8 -*-`` をソースの最初につけてみてはいかがでしょう。
27
+ もし、日本語を含むソースコードになっていたら、``# -*- coding: utf-8 -*-`` をソースの最初につけてみてはいかがでしょう。
28
+
29
+
30
+ ### コメントをいただいての追記
31
+
32
+ コメントに貼っていただいた pythonPath: のところは最初の投稿と違いますね。
33
+ ここは、ターミナルで "which python" と入力したときに表示されるものを貼ってくださいね。
34
+
35
+ わたしの環境だと、こうなりますので、
36
+
37
+ ```bash
38
+ % which python
39
+ /usr/local/bin/python
40
+ ```
41
+
42
+ こちらを使って、JavaScript側はこんな感じになります。
43
+
44
+ ```JavaScript
45
+ let { PythonShell } = require('python-shell');
46
+
47
+ const envelope = {"latitude":"15","longitude":"45","spot":"ああ"};
48
+
49
+ let options = {
50
+ mode: 'text',
51
+ pythonPath: '/usr/local/bin/python', // which python で表示された結果を指定してください!
52
+ pythonOptions: ['-u'], // get print results in real-time
53
+ scriptPath: '/home/kakimoto/M1/web/',
54
+ args: [envelope.latitude, envelope.longitude, envelope.spot]
55
+ };
56
+
57
+ PythonShell.run('test.py', options, function (err, results) {
58
+ if (err) throw err;
59
+ // results is an array consisting of messages collected during execution
60
+ console.log('results: %j', results);
61
+ });
62
+ ```
63
+
64
+ うまくいくと、こうなると思います....。
65
+
66
+ ```bash
67
+ node test.js
68
+ results: ["60.0","ああ"]
69
+ ```