首页 > 解决方案 > 为什么我在 node.js 中调用 python-shell 时出现 TypeError?

问题描述

我正在尝试在 node.js 中运行 Python 程序,我的代码:

import {PythonShell} from 'python-shell';

var pyshell =  PythonShell.run('./myscript.py', options, 
              function(err:any, results:any)
              {
                  if (err) throw err;
                  else console.log(results);
              });
              
              pyshell.on('message', function (message: any) {
                // received a message sent from the Python script (a simple "print" statement)
                console.log(message);
            });
            
            // end the input stream and allow the process to exit
            pyshell.end(function (err: any) {
                if (err){
                    throw err;
                };
            
                console.log('finished');
            });

但我得到了错误:

TypeError: The "original" argument must be of type Function  promisify .../node_modules/util/util.js:601

所以我想这可能与 PythonShell 的定义方式有关,即不是函数而是对象。但不知道如何修复它。我也试过:

const {PythonShell} = require('python-shell');

标签: pythonnode.js

解决方案


推荐阅读