Having a Node.js Lambda Function I want to run a Python script using Node.js' child_process command.
I followed this AWS tutorial and included the following at the start of my .js function:
process.env[‘PATH’] = process.env[‘PATH’] + ‘:’ + process.env[‘LAMBDA_TASK_ROOT’];
And the main Python script call starts here:
const { spawn } = require('child_process');
function calculateSomething(next) {
var script = spawn('python', ['my_python_script.py', args]);
script.stdout.on('data', (data) => { doSomething(data); });
script.on('error', (error) => { console.error(error); });
script.on('exit', () => { next(); });
}
The Python script runs using an external library (e.g., numpy), so I have to install it using pip.
Now:
process.env[‘PATH’] = process.env[‘PATH’] + ‘:’ + process.env[‘LAMBDA_TASK_ROOT’];
at the start of my .js function?It's similar to this other question.
This is the simple implementation of a how-to run python script with Node. js which can be useful in situations where you have a stack of Node. js application and you want to run a simple python script. If you want to know more about PythonShell module, then go through the given link.
Python packages that contain compiled code (for example: NumPy and pandas) aren't always compatible with Lambda runtimes by default. If you install these packages using pip, then the packages download and compile a module-name package for the architecture of the local machine.
You need a custom runtime to run multiple languages
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With