在 Electron 应用程序中运行 python 脚本 [英] Run python script in Electron app

查看:111
本文介绍了在 Electron 应用程序中运行 python 脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Electron 项目,它使用 NodeJS 的 child_process 模块执行一些 python 脚本.我的 python 脚本位于项目的根文件夹中.

I have a Electron project which executes some python script using NodeJS's child_process module. My python script is in the root folder of my project.

以下是我如何调用 python 脚本:

Here's how I call the python script:

let py = spawn('python',['ResolvePosition.py', obsFilePath, navFilePath])
py.stdout.on('data', data => console.log('data : ', data.toString()))
py.on('close', ()=>{
  // Python ends, do stuff
})

如果我使用 npm start 运行我的电子应用程序,这很好用当我使用 npm 模块 electron-builder 为 Windows 构建可执行文件并从 dist/win-unpacked/my-app.exe 运行可执行文件时,这将不起作用,似乎我的脚本无法使用 python ./my-script-py.

This works fine if I run my electron app with npm start When I build an executable for Windows using the npm module electron-builder and run the executable from dist/win-unpacked/my-app.exe, this won't work, it seems that my script is not accesible with python ./my-script-py.

那么,我怎样才能使这个代码适用于构建的项目?

So, how can I make this code works for the built project?

推荐答案

您可以将脚本包含在 extraResources:

You can include the script in extraResources:

"build": {
    "extraResources": "python_scripts",
...

然后目录将位于应用根目录:

And then the dir will be at the app root:

let python = spawn('python', [path.join(app.getAppPath(), '..', 'python_scripts/my_script.py'])

这篇关于在 Electron 应用程序中运行 python 脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆