我正在尝试将 Python 脚本与 NodeJS 结合使用。下面的代码有效,但我需要一种方法在“npm install”上执行“pip install pandas”
Node
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()
最佳答案
您可以在 package.json
文件中使用 scripts
的 postinstall
属性来执行此类操作。这是一个小的演示代码。(假设 pip 命令可以在你的 cmd 中工作)
"scripts": {
"postinstall": "pip install pandas"
}
关于python - 为使用 nodejs 调用的脚本导入 python 包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52419935/