未捕获的 ReferenceError:未定义 PythonShell - Nodejs 和 Electron 的 PythonShell

问题描述

一旦在电子中使用 PythonShell 按下按钮,我就会尝试运行 Python 脚本。

我在 ma​​in.js 中有我的导入,我通过一个单独的 js 文件调用 python 脚本的打开。该文件采用函数形式,名为 pybutton.js。 pybutton.js 文件是通过 HTML 中的 <script> 标记添加的。

我的 main.js:

import {app,browserWindow} from 'electron'
import {PythonShell} from 'python-shell'
const path = require('path')

let mainWindow

function createWindow () {
  // Create the browser window.
  mainWindow = new browserWindow({
    width: 1100,height: 600,webPreferences: {
      nodeIntegration: true
    }
  })

  // and load the index.html of the app.
  mainWindow.loadFile('index.html')

  //DevTools.
  mainWindow.webContents.openDevTools()

  // Emitted when the window is closed.
  mainWindow.on('closed',function () {
    mainWindow = null
  })
}

// This method will be called when Electron has finished initialization
app.on('ready',createWindow)

// Quit when all windows are closed. additional code for mac yuck.
app.on('window-all-closed',function () {
  if (process.platform !== 'darwin') app.quit()
})
app.on('activate',function () {
  if (mainWindow === null) createWindow()
})

我的 HTML:

<!DOCTYPE html>
<html lang="en">
    <head>
        <Meta charset="UTF-8">
        <title>Nexit</title>
        <script type="text/javascript" src="main.js"></script>
        <script src="js/pybutton.js"></script> -->
    </head>
    <body>
        <p>Text!</p>
        <input type="button" class"submit" value="Python" onclick="pybutton()">
        <br>
        <img id="img" src="">
        <img id="imgp" src="">
    </body>
</html>

我的 pybutton.js:

function pybutton() {
    let options = {
        mode: 'text',pythonoptions: ['-u'],// get print results in real-time
        scriptPath: '/../py'
    };

    PythonShell.run('pybutton.py',options,function (err,results) {
        if (err) throw err;
        // results is an array consisting of messages collected during execution
        console.log('results of pybutton: %j',results);
    });
}

单击按钮时,出现此错误

Uncaught ReferenceError: PythonShell is not defined
    at pybutton (pybutton.js:8)
    at HTMLInputElement.onclick (index.html)

我将如何再次定义 PythonShell?

编辑:这是我的 renderer.js:

import {log} from 'console'

const path = require('path');

log('Hello from the renderer process!')

//-------------------------------------------------------------

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


const path = require('path');

然后我添加了 到index.html

我继续收到同样的错误

Uncaught ReferenceError: PythonShell is not defined
    at pybutton (pybutton.js:8)
    at HTMLInputElement.onclick (index.html:21) 

所以这仍然发生在我上面 pybutton.js 的第 8 行。我一定没有在渲染器中正确定义东西。

使用的节点模块:

  • 电子 4.1.2
  • esm 3.2.25
  • python-shell 2.0.3

解决方法

您收到此错误(“PythonShell 未定义”),因为实际上您没有在渲染器进程中定义和导入 PythonShell 包。

您正在主进程中加载​​ PythonShell,然后启动 BrowserWindow,默认情况下它无法访问节点包。但是,由于您已经通过将 nodeIntegration 设置为 true 来更改它,因此您现在应该能够在渲染器进程中轻松导入 PythonShell,而不是在主进程中。

请注意,您可能还需要在选项中提供完整的(绝对)scriptPath

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...