我正在尝试将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()
答案 0 :(得分:2)
您可以在postinstall
文件中使用scripts
的{{1}}属性来执行此操作。这是用于此的小演示代码。(假设pip命令将在您的cmd中运行)
package.json