I want to invoke a python script in a js file using python-shell. However the script requires python3 and py2 is always used, while my local development has both py2 and 3 installed. How do i specify it to use python3 instead?
app.get('/run_py', (req, res)=>{
var myPythonScriptPath = 'script.py';
// Use python shell
var PythonShell = require('python-shell');
var pyshell = new PythonShell("pyscripts/test.py");
pyshell.on('message', function (message) {
// received a message sent from the Python script (a simple "print" statement)
console.log(message);
});
// end the input stream and allow the process to exit
pyshell.end(function (err) {
if (err){
throw err;
};
//console.log('finished');
});
});
My py script looks like:
import sys
if sys.version_info[0] < 3:
raise Exception("Must be using Python 3")
print('running test.py')
print (sys.version_info[0])
It always uses python 2.7 when running locally.
Moreover, when I push my code to heroku server which use python 3.6 as default. The scripts runs perfectly...
Any insights? please?
EDIT: I saw you can specify the python path in the PythonShell.run as a parameter.. But what if different platforms say heroku and my local machine has different path to python?