4

I'm trying to deploy a node.js app that calls a python script for background tasks. The way I'm implementing it is via python-shell:

var pythonShell = require('python-shell');

var options = {
    pythonPath: 've-env/bin/python3',
    args:
    [
        req.query.term,
        req.params.id,
        req.session.user.searchName,
        req.session.user.searchKey
    ]
};

pythonShell.run('VideoAnalyzer/Search.py', options, function (err, data) {
    if (err) 
        throw err ;
    var values = JSON.parse(data[0]).value;
    var resultsByRel = values;
    res.render('video', {resultsByRel: resultsByRel, resultsByTime: [], searchTerm: req.query.term, url: req.query.url});
});

The path to python is specified in options.pythonPath (in a python virtual environment called 've-env').

This works in my local environment. However, when I deployed my app to Azure App Service, I got the following error message:

Error: spawn Unknown system error -8
at _errnoException (util.js:992:11)
at ChildProcess.spawn (internal/child_process.js:323:11)
at exports.spawn (child_process.js:502:9)
at new PythonShell (/home/site/wwwroot/node_modules/python-shell/index.js:59:25)
at Function.PythonShell.run (/home/site/wwwroot/node_modules/python-shell/index.js:160:19)
at Object.exports.search_result_video (/home/site/wwwroot/controllers/searchController.js:20:15)
at /home/site/wwwroot/routes/video.js:15:21
at Layer.handle [as handle_request] (/home/site/wwwroot/node_modules/express/lib/router/layer.js:95:5)
at next (/home/site/wwwroot/node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (/home/site/wwwroot/node_modules/express/lib/router/route.js:112:3)

The app is deployed in Linux environment with node version v8.9.

Is there any python environment configuration I should have performed before deployment?

1
  • Hi,any updates ?Does my answer helps you? Commented Aug 22, 2018 at 1:21

1 Answer 1

4

Is there any python environment configuration I should have performed before deployment?

The answer is yes. If you want to run python script in Azure App Service,you need to install python environment in your application.Please refer to the steps below:

Please refer to my work steps and see if the error still shows up.

Step 1 : Add Extensions on the portal(here is Python 3.6.1 x64)

enter image description here

Step 2: Switch to the Kudu CMD and commands cd Python364x64 and touch get-pip.py and copy the content of the url https://bootstrap.pypa.io/get-pip.py into the get-pip.py via Edit button, then run python get-pip.py to install the pip tool.

enter image description here

Step 3 : Install any packages you need via python -m pip install requests

enter image description here

Then you need to modify the python configuration of your code:

var express = require('express');
var pythonShell = require('python-shell');
var router = express.Router();

var options = {
  pythonPath: 'D:/home/python364x64/python',
  scriptPath: 'D:/home/site/wwwroot'
  // args:
  // [
  //     req.query.term,
  //     req.params.id,
  //     req.session.user.searchName,
  //     req.session.user.searchKey
  // ]
};

var resultsByRel;

pythonShell.run('TestRequests.py', options, function (err, data) {
  if (err) throw err;
  // results is an array consisting of messages collected during execution
  var values = JSON.parse(data[0]);
  resultsByRel = values;
  console.log('results: %j', resultsByRel);
});

router.get('/', function(req, res, next) {
  res.send(resultsByRel);
  // res.render('executePython', resultsByRel );  
});

module.exports = router;

My simple python script:

import requests

r= requests.get("https://www.bing.com")
print (r.status_code)

Hope it helps you. Any concern ,please let me know.

Sign up to request clarification or add additional context in comments.

2 Comments

FYI, for post year 2021 or so, you don't need to add python. It's already installed. Instead of downloading the file in the url as shown, you might need the 2.7 version. The script will tell you it's the wrong version if so and give you the new link. After that, your python path only needs to be 'python'. Nothing else.
Unfortunately, you will have to do this after every deployment.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.