gpt4 book ai didi

python - Electron 建立后,python-shell的pythonPath不起作用

转载 作者:行者123 更新时间:2023-12-03 12:40:56 24 4
gpt4 key购买 nike

我正在使用 Electron 应用程序使用python-shell运行python脚本。我已经将pythonPath设置为我的anaconda env,如下所示。当我运行该应用程序时,此方法可以找到,但是用 Electron 构建器打包该应用程序后,它显示以下错误:即使我在同一台PC上使用绝对路径,也找不到这样的文件或目录。

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

const options = {
mode: 'text',
pythonPath: 'C:/Users/nauma/.conda/envs/YOLO/python.exe',
pythonOptions: ['-u'], // get print results in real-time
scriptPath: path.join(__dirname, '/build/engine/')
};

PythonShell.run('index.py', options, function (err, res) {
if (err) throw err;
console.log(res);
});

构建应用程序后的异常如下所示:

[

最佳答案

如果未设置pythonPath,则在Windows上为



  • python shell可以自行找到python的路径,

    可能使用env var或注册表项。
  • 但是要确保我的 Electron 应用程序可以在任何地方运行,

    我更喜欢使用特定的配置来嵌入python

    Electron 制造商将python
  • bundle 在一起

    package.json:
    "build": {
    "appId": "angular_electron.id",
    "extraFiles": [
    {
    "from": "resources/${os}",
    "to": "Resources",
    }
    ]
    }

    并使用正确的resourcepath调用python:
    import { isPackaged } from 'electron-is-packaged';
    const resourcesPath = isPackaged
    ? join(rootPath, './resources')
    : join(rootPath, './resources', getPlatform());

    pyshell.PythonShell.run('hello.py',
    {pythonPath:join(resourcesPath,'/python-3.6.3-embed-amd64/python.exe'),scriptPath:join(resourcesPath,'/bin')},
    function (err, results) {
    if (err) throw err;
    console.log('hello.py finished.');
    console.log('results:', results);
    });

    关于python - Electron 建立后,python-shell的pythonPath不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61043441/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com