You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+50-21Lines changed: 50 additions & 21 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -25,8 +25,8 @@ npm test
25
25
26
26
### Running a Python script:
27
27
28
-
```js
29
-
varPythonShell=require('python-shell');
28
+
```typescript
29
+
import {PythonShell} from'python-shell';
30
30
31
31
PythonShell.run('my_script.py', function (err) {
32
32
if (err) throwerr;
@@ -38,10 +38,10 @@ If the script writes to stderr or exits with a non-zero code, an error will be t
38
38
39
39
### Running a Python script with arguments and options:
40
40
41
-
```js
42
-
varPythonShell=require('python-shell');
41
+
```typescript
42
+
import {PythonShell} from'python-shell';
43
43
44
-
var options = {
44
+
let options = {
45
45
mode: 'text',
46
46
pythonPath: 'path/to/python',
47
47
pythonOptions: ['-u'], // get print results in real-time
@@ -58,9 +58,9 @@ PythonShell.run('my_script.py', options, function (err, results) {
58
58
59
59
### Exchanging data between Node and Python:
60
60
61
-
```js
62
-
varPythonShell=require('python-shell');
63
-
var pyshell =newPythonShell('my_script.py');
61
+
```typescript
62
+
import {PythonShell} from'python-shell';
63
+
let pyshell =newPythonShell('my_script.py');
64
64
65
65
// sends a message to the Python script via stdin
66
66
pyshell.send('hello');
@@ -92,9 +92,10 @@ For more details and examples including Python source code, take a look at the t
92
92
93
93
### Error Handling and extended stack traces
94
94
95
-
An error will be thrown if the process exits with a non-zero exit code or if data has been written to stderr. Additionally, if "stderr" contains a formatted Python traceback, the error is augmented with Python exception details including a concatenated stack trace.
95
+
An error will be thrown if the process exits with a non-zero exit code. Additionally, if "stderr" contains a formatted Python traceback, the error is augmented with Python exception details including a concatenated stack trace.
96
96
97
97
Sample error with traceback (from test/python/error.py):
traceback: 'Traceback (most recent call last):\n File "test/python/error.py", line 6, in <module>\n divide_by_zero()\n File "test/python/error.py", line 4, in divide_by_zero\n print 1/0\nZeroDivisionError: integer division or modulo by zero\n',
110
113
executable: 'python',
@@ -113,7 +116,9 @@ would result into the following error:
113
116
args: null,
114
117
exitCode: 1 }
115
118
```
119
+
116
120
and `err.stack` would look like this:
121
+
117
122
```
118
123
Error: ZeroDivisionError: integer division or modulo by zero
119
124
at PythonShell.parseError (python-shell/index.js:131:17)
@@ -141,6 +146,7 @@ Creates an instance of `PythonShell` and starts the Python process
141
146
*`binary`: data is streamed as-is through `stdout` and `stdin`
142
147
*`formatter`: each message to send is transformed using this method, then appended with "\n"
143
148
*`parser`: each line of data (ending with "\n") is parsed with this function and its result is emitted as a message
149
+
*`stderrParser`: each line of logs (ending with "\n") is parsed with this function and its result is emitted as a message
144
150
*`encoding`: the text encoding to apply on the child process streams (default: "utf8")
145
151
*`pythonPath`: The path where to locate the "python" executable. Default: "python"
146
152
*`pythonOptions`: Array of option switches to pass to "python"
@@ -154,23 +160,25 @@ PythonShell instances have the following properties:
154
160
*`command`: the full command arguments passed to the Python executable
155
161
*`stdin`: the Python stdin stream, used to send data to the child process
156
162
*`stdout`: the Python stdout stream, used for receiving data from the child process
157
-
*`stderr`: the Python stderr stream, used for communicating errors
163
+
*`stderr`: the Python stderr stream, used for communicating logs & errors
158
164
*`childProcess`: the process instance created via `child_process.spawn`
159
165
*`terminated`: boolean indicating whether the process has exited
160
166
*`exitCode`: the process exit code, available after the process has ended
161
167
162
168
Example:
163
-
```js
169
+
170
+
```typescript
164
171
// create a new instance
165
-
var shell =newPythonShell('script.py', options);
172
+
let shell =newPythonShell('script.py', options);
166
173
```
167
174
168
175
#### `#defaultOptions`
169
176
170
177
Configures default options for all new instances of PythonShell.
@@ -182,7 +190,8 @@ Runs the Python script and invokes `callback` with the results. The callback con
182
190
This method is also returning the `PythonShell` instance.
183
191
184
192
Example:
185
-
```js
193
+
194
+
```typescript
186
195
// run a simple script
187
196
PythonShell.run('script.py', function (err, results) {
188
197
// script finished
@@ -194,20 +203,25 @@ PythonShell.run('script.py', function (err, results) {
194
203
Sends a message to the Python script via stdin. The data is formatted according to the selected mode (text or JSON), or through a custom function when `formatter` is specified.
195
204
196
205
Example:
197
-
```js
206
+
207
+
```typescript
198
208
// send a message in text mode
199
-
var shell =newPythonShell('script.py', { mode:'text '});
209
+
let shell =newPythonShell('script.py', { mode: 'text '});
200
210
shell.send('hello world!');
201
211
202
212
// send a message in JSON mode
203
-
var shell =newPythonShell('script.py', { mode:'json '});
213
+
let shell =newPythonShell('script.py', { mode: 'json '});
Parses incoming data from the Python script written via stdout and emits `message` events. This method is called automatically as data is being received from stdout.
210
220
221
+
#### `.receiveStderr(data)`
222
+
223
+
Parses incoming logs from the Python script written via stderr and emits `stderr` events. This method is called automatically as data is being received from stderr.
224
+
211
225
#### `.end(callback)`
212
226
213
227
Closes the stdin stream, allowing the Python script to finish and exit. The optional callback is invoked when the process is terminated.
@@ -221,20 +235,35 @@ Terminates the python script, the optional end callback is invoked if specified.
221
235
Fires when a chunk of data is parsed from the stdout stream via the `receive` method. If a `parser` method is specified, the result of this function will be the message value. This event is not emitted in binary mode.
222
236
223
237
Example:
224
-
```js
238
+
239
+
```typescript
225
240
// receive a message in text mode
226
-
var shell =newPythonShell('script.py', { mode:'text '});
241
+
let shell =newPythonShell('script.py', { mode: 'text '});
227
242
shell.on('message', function (message) {
228
243
// handle message (a line of text from stdout)
229
244
});
230
245
231
246
// receive a message in JSON mode
232
-
var shell =newPythonShell('script.py', { mode:'json '});
247
+
let shell =newPythonShell('script.py', { mode: 'json '});
233
248
shell.on('message', function (message) {
234
249
// handle message (a line of text from stdout, parsed as JSON)
235
250
});
236
251
```
237
252
253
+
#### event: `stderr`
254
+
255
+
Fires when a chunk of logs is parsed from the stderr stream via the `receiveStderr` method. If a `stderrParser` method is specified, the result of this function will be the message value. This event is not emitted in binary mode.
256
+
257
+
Example:
258
+
259
+
```typescript
260
+
// receive a message in text mode
261
+
let shell =newPythonShell('script.py', { mode: 'text '});
262
+
shell.on('stderr', function (stderr) {
263
+
// handle stderr (a line of text from stderr)
264
+
});
265
+
```
266
+
238
267
#### event: `close`
239
268
240
269
Fires when the process has been terminated, with an error or not.
0 commit comments