javascript - npm python-shell 不适用于 Electron

标签 javascript python npm electron desktop-application

我正在尝试使用 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我收到以下错误:

enter image description here

有人知道为什么这不起作用吗?我看到很多关于 Electron 的文章,它们都做同样的事情并且有效。如果我遗漏了什么,请告诉我。

谢谢。

最佳答案

var python = require("python-shell")

更改为

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

关于javascript - npm python-shell 不适用于 Electron ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52746470/

相关文章:

javascript - 如何使用 meteor 模板中的语句

python - 在 python 中使用正则表达式捕获所有连续的全大写单词?

python - flask 属性错误 : module 'app' has no attribute 'run'

python - 使用 OpenCV 对图像进行 mask 和平滑处理

node.js - npm 中的 "production"标志有什么用?

javascript - 在 jQuery 中添加表格行

javascript - 使用 JS 无法按预期工作将 onclick 事件分配给多个跨度

JavaScript 音频对象在 Android API 19 中不起作用

javascript - 如何创建智能局域网或公司 npm 注册表(node.js 包存储库)?

javascript - 引用错误: require is not defined @react