繁体   English   中英

来自 Node.js 的 python-shell 不执行 python 脚本

[英]python-shell from Node.js not executing python scripts

我正在尝试学习 JavaScript。 以前用 Python 编码。 我喜欢将我的一些 Python 程序链接起来与 Node.js 一起运行。 使用 python-shell 中的代码运行我的 Python 脚本,但遇到了以下我什至无法理解的错误:

/Users/cadellteng/AtomProjects/useSpawn/node_modules/python-shell/index.js:217
                return callback(err);
                       ^

TypeError: callback is not a function
    at PythonShell._endCallback (/Users/cadellteng/AtomProjects/useSpawn/node_modules/python-shell/index.js:217:24)
    at terminateIfNeeded (/Users/cadellteng/AtomProjects/useSpawn/node_modules/python-shell/index.js:158:39)
    at ChildProcess.<anonymous> (/Users/cadellteng/AtomProjects/useSpawn/node_modules/python-shell/index.js:131:13)
    at ChildProcess.emit (events.js:316:20)
    at Process.ChildProcess._handle.onexit (internal/child_process.js:275:12)

我只是想运行一个简单的节点和分别包含 5 行和 2 行的 python 脚本。 请查看下面的代码,如果我做错了什么,请告诉我:

使用PythonShell.js

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

PythonShell.run('hello.py', () => {
  console.log('finished');
});

你好.py

name = input('What is your name?\n>>> ') # John Doe
print('Hello '+ name +'!') # expects output as 'Hello John Doe!'

我试图将我的 python 放在main() ,但它似乎不起作用。

我相信 python 脚本可以正常工作,因为当我从终端执行它时它执行得很好。

查看文档

PythonShell.run的参数是脚本名称、选项对象和回调。 所以回调是第三个参数。

在您的情况下,您将回调传递到选项应该所在的位置。 第三个参数仍然是undefined ,实际上它不是一个函数。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM