为使用nodejs调用的脚本导入python包

时间:2018-09-20 07:36:08

标签: python node.js npm pip

我正在尝试将Python脚本与NodeJS一起使用。下面的代码有效,但是我需要一种在“ npm install”上执行“ pip install pandas”的方法

nodeJS

router.get('/', (req, res) => {
  const filePath = 'python/testing2.py' 
  const spawn = require("child_process").spawn;
  const pythonProcess = spawn('python3',[filePath, '-l']); 

  util.log('readingin')
  pythonProcess.stdout.on('data', (data) => { 
    const textChunk = data.toString('utf8');// buffer to string
    util.log(textChunk);
    res.json({'working': true, 'data': textChunk})
  });
});

python:

import sys 
from pandas import read_csv
from pandas import datetime    

def parser(x):
    return datetime.strptime('190'+x, '%Y-%m')    

print("Output from Python") 
series = read_csv('shampoo-sales.csv', header=0, parse_dates=[0], index_col=0, squeeze=True, date_parser=parser)
print (series)
sys.stdout.flush()

1 个答案:

答案 0 :(得分:2)

您可以在postinstall文件中使用scripts的{​​{1}}属性来执行此操作。这是用于此的小演示代码。(假设pip命令将在您的cmd中运行)

package.json