我正在尝试使用 npm 的 python-shell 包将 python 脚本与 Electron 应用程序链接起来。
条件是每当我单击按钮时就执行脚本。所以,假设我的目录包含:
main.js (electron file)
index.js (my script)
index.html
main.py
在index.js中,我编写以下代码来导入python-shell并执行main.py脚本:
function get_input() {
var python = require("python-shell")
var path = require("path")
var options = {
scriptPath: path.join(__dirname, './python/')
}
var weather = new python('main.py', options);
weather.on('message', function (message) {
console.log(message);
})
}
在index.html中,我创建了一个按钮标签来在单击时运行此功能:
<button onclick="get_input()">Go!</button>
在 main.py 中,我现在所做的就是打印“hello”。现在,当我运行时,npm start我收到以下错误:
有人知道为什么这不起作用吗?我看到很多关于 Electron 的文章,它们都做同样的事情并且有效。如果我遗漏了什么,请告诉我。
谢谢。
最佳答案
将 var python = require("python-shell")
更改为
const {PythonShell} = require("python-shell");
关于javascript - npm python-shell 不适用于 Electron ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52746470/