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

标签 python node.js npm pip

我正在尝试将 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 文件中使用 scriptspostinstall 属性来执行此类操作。这是一个小的演示代码。(假设 pip 命令可以在你的 cmd 中工作)

"scripts": {
 "postinstall": "pip install pandas"
}

关于python - 为使用 nodejs 调用的脚本导入 python 包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52419935/

相关文章:

Python - 根据类型将字符过滤到结构中

python - 如何使用 Python 将多个 Excel 工作表从网站下载到 Pandas DataFrame 中

c# - C# 中的 IronPython 和 Nodebox

node.js - sequelize 交易中的 setShooter 是什么?

javascript - 将现有的 JS "package"与 Node.JS 一起使用吗?

python - 如何在序列化大量 GeoDjango 几何字段时进行性能优化?

node.js - Sequelize addColumn 迁移始终引用公共(public)架构

javascript - 如何为每个指定(node env,express fr)路由发布请求创建可以变异并可通过所有模块使用的全局 obj 实例?

npm - 我可以在NPM上安装Polymer(1.0)吗?

reactjs - 如何打开 react-devtools?