졸업프로젝트

[Node.js] python-shell 이용하기

valid_ming 2021. 4. 10. 19:21

이용하기 위해서, 모듈을 설치한다.

>> npm install python-shell

 

옵션에서 넘겨줄 인자를 선택할 수 있고 콜백함수에서 return된 데이터를 확인할 수 있다.

 

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

var options = {
  mode: 'text',
  pythonPath: '',
  pythonOptions: ['-u'],
  scriptPath: '',
  args: [arg1,arg2]
};

pythonShell.PythonShell.run('실행스크립트', options, function (err, results) {
      if (err) console.log(err);
      else console.log(results);
});

 

파이썬 스크립트에서는 sys.argv를 이용하여 받은 인자를 확인한다.

sys.argv의 0번째 값은 실행 스크립트이니 1번째 값이 arg1이다.

import sys

print sys.argv