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
+22-89
Original file line number
Diff line number
Diff line change
@@ -23,38 +23,25 @@ npm test
23
23
24
24
## Documentation
25
25
26
-
### Running python code:
27
-
28
-
```typescript
29
-
import {PythonShell} from'python-shell';
30
-
31
-
PythonShell.runString('x=1+1;print(x)', function (err) {
32
-
if (err) throwerr;
33
-
console.log('finished');
34
-
});
35
-
```
36
-
37
-
If the script exits with a non-zero code, an error will be thrown.
38
-
39
26
### Running a Python script:
40
27
41
-
```typescript
42
-
import {PythonShell} from'python-shell';
28
+
```js
29
+
varPythonShell=require('python-shell');
43
30
44
31
PythonShell.run('my_script.py', function (err) {
45
32
if (err) throw err;
46
33
console.log('finished');
47
34
});
48
35
```
49
36
50
-
If the script exits with a non-zero code, an error will be thrown.
37
+
If the script writes to stderr or exits with a non-zero code, an error will be thrown.
51
38
52
39
### Running a Python script with arguments and options:
53
40
54
-
```typescript
55
-
import {PythonShell} from'python-shell';
41
+
```js
42
+
varPythonShell=require('python-shell');
56
43
57
-
let options = {
44
+
var options = {
58
45
mode:'text',
59
46
pythonPath:'path/to/python',
60
47
pythonOptions: ['-u'], // get print results in real-time
@@ -71,9 +58,9 @@ PythonShell.run('my_script.py', options, function (err, results) {
71
58
72
59
### Exchanging data between Node and Python:
73
60
74
-
```typescript
75
-
import {PythonShell} from'python-shell';
76
-
let pyshell =newPythonShell('my_script.py');
61
+
```js
62
+
varPythonShell=require('python-shell');
63
+
var pyshell =newPythonShell('my_script.py');
77
64
78
65
// sends a message to the Python script via stdin
79
66
pyshell.send('hello');
@@ -105,10 +92,9 @@ For more details and examples including Python source code, take a look at the t
105
92
106
93
### Error Handling and extended stack traces
107
94
108
-
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.
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.
109
96
110
97
Sample error with traceback (from test/python/error.py):
ZeroDivisionError: integer division or modulo by zero
119
105
```
120
-
121
106
would result into the following error:
122
-
123
-
```typescript
107
+
```js
124
108
{ [Error: ZeroDivisionError: integer division or modulo by zero]
125
109
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',
126
110
executable:'python',
@@ -129,9 +113,7 @@ would result into the following error:
129
113
args:null,
130
114
exitCode:1 }
131
115
```
132
-
133
116
and `err.stack` would look like this:
134
-
135
117
```
136
118
Error: ZeroDivisionError: integer division or modulo by zero
137
119
at PythonShell.parseError (python-shell/index.js:131:17)
@@ -159,7 +141,6 @@ Creates an instance of `PythonShell` and starts the Python process
159
141
*`binary`: data is streamed as-is through `stdout` and `stdin`
160
142
*`formatter`: each message to send is transformed using this method, then appended with "\n"
161
143
*`parser`: each line of data (ending with "\n") is parsed with this function and its result is emitted as a message
162
-
*`stderrParser`: each line of logs (ending with "\n") is parsed with this function and its result is emitted as a message
163
144
*`encoding`: the text encoding to apply on the child process streams (default: "utf8")
164
145
*`pythonPath`: The path where to locate the "python" executable. Default: "python"
165
146
*`pythonOptions`: Array of option switches to pass to "python"
@@ -173,25 +154,23 @@ PythonShell instances have the following properties:
173
154
*`command`: the full command arguments passed to the Python executable
174
155
*`stdin`: the Python stdin stream, used to send data to the child process
175
156
*`stdout`: the Python stdout stream, used for receiving data from the child process
176
-
*`stderr`: the Python stderr stream, used for communicating logs & errors
157
+
*`stderr`: the Python stderr stream, used for communicating errors
177
158
*`childProcess`: the process instance created via `child_process.spawn`
178
159
*`terminated`: boolean indicating whether the process has exited
179
160
*`exitCode`: the process exit code, available after the process has ended
180
161
181
162
Example:
182
-
183
-
```typescript
163
+
```js
184
164
// create a new instance
185
-
let shell =newPythonShell('script.py', options);
165
+
var shell =newPythonShell('script.py', options);
186
166
```
187
167
188
168
#### `#defaultOptions`
189
169
190
170
Configures default options for all new instances of PythonShell.
@@ -203,53 +182,32 @@ Runs the Python script and invokes `callback` with the results. The callback con
203
182
This method is also returning the `PythonShell` instance.
204
183
205
184
Example:
206
-
207
-
```typescript
185
+
```js
208
186
// run a simple script
209
187
PythonShell.run('script.py', function (err, results) {
210
188
// script finished
211
189
});
212
190
```
213
191
214
-
#### `#runString(code, options, callback)`
215
-
216
-
Runs the Python code and invokes `callback` with the results. The callback contains the execution error (if any) as well as an array of messages emitted from the Python script.
217
-
218
-
This method is also returning the `PythonShell` instance.
219
-
220
-
Example:
221
-
222
-
```typescript
223
-
// run a simple script
224
-
PythonShell.run('x=1;print(x)', function (err, results) {
225
-
// script finished
226
-
});
227
-
```
228
-
229
192
#### `.send(message)`
230
193
231
194
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.
232
195
233
196
Example:
234
-
235
-
```typescript
197
+
```js
236
198
// send a message in text mode
237
-
let shell =newPythonShell('script.py', { mode: 'text '});
199
+
var shell =newPythonShell('script.py', { mode:'text '});
238
200
shell.send('hello world!');
239
201
240
202
// send a message in JSON mode
241
-
let shell =newPythonShell('script.py', { mode: 'json '});
203
+
var 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.
248
210
249
-
#### `.receiveStderr(data)`
250
-
251
-
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.
252
-
253
211
#### `.end(callback)`
254
212
255
213
Closes the stdin stream, allowing the Python script to finish and exit. The optional callback is invoked when the process is terminated.
@@ -258,50 +216,25 @@ Closes the stdin stream, allowing the Python script to finish and exit. The opti
258
216
259
217
Terminates the python script, the optional end callback is invoked if specified. A kill signal may be provided by `signal`, if `signal` is not specified SIGTERM is sent.
260
218
261
-
#### `checkSyntax(code:string)`
262
-
263
-
Checks the syntax of the code and returns a promise.
264
-
Promise is rejected if there is a syntax error.
265
-
266
-
#### `checkSyntaxFile(filePath:string)`
267
-
268
-
Checks the syntax of the file and returns a promise.
269
-
Promise is rejected if there is a syntax error.
270
-
271
219
#### event: `message`
272
220
273
221
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.
274
222
275
223
Example:
276
-
277
-
```typescript
224
+
```js
278
225
// receive a message in text mode
279
-
let shell =newPythonShell('script.py', { mode: 'text '});
226
+
var shell =newPythonShell('script.py', { mode:'text '});
280
227
shell.on('message', function (message) {
281
228
// handle message (a line of text from stdout)
282
229
});
283
230
284
231
// receive a message in JSON mode
285
-
let shell =newPythonShell('script.py', { mode: 'json '});
232
+
var shell =newPythonShell('script.py', { mode:'json '});
286
233
shell.on('message', function (message) {
287
234
// handle message (a line of text from stdout, parsed as JSON)
288
235
});
289
236
```
290
237
291
-
#### event: `stderr`
292
-
293
-
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.
294
-
295
-
Example:
296
-
297
-
```typescript
298
-
// receive a message in text mode
299
-
let shell =newPythonShell('script.py', { mode: 'text '});
300
-
shell.on('stderr', function (stderr) {
301
-
// handle stderr (a line of text from stderr)
302
-
});
303
-
```
304
-
305
238
#### event: `close`
306
239
307
240
Fires when the process has been terminated, with an error or not.
0 commit comments