Skip to content
Advertisement

exchanging data between node and Python with python-shell

I am trying to learn how to exchange data between Node and Python with python-shell, on the git repo they have some example code:

Borrowing some of this code, this is app.js:

JavaScript

And this is my_script.py below that will just print the weekday and current time:

JavaScript

When run app.js this throws an error:

JavaScript

Any ideas to try? Thanks for any tips not a lot of wisdom here. Can I call these functions on the Python script through python-shell? Just curious if I could use Javascript with this python-shell package to retrieve either the current weekday or current time from Python.

Advertisement

Answer

Replace import {PythonShell} from 'python-shell'; with let {PythonShell} = require('python-shell');

Edit: In .py file, import sys and then the arguments passed from nodejs will be available in sys.argv as array. I suppose by putting if checks you can call specific functions and at the end whatever you print in python would be available in results in js

Sample:

.py file:

JavaScript

.js file:

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement