Run python script in Electron app(在 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 脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 Electron 应用程序中运行 python 脚本
基础教程推荐
- 将 x 轴刻度更改为自定义字符串 2022-01-01
- 由Python将MP3转换为MIDI(类型错误:无法加载插件:mtg-Melodia:Melodia) 2022-01-01
- 用 Python 编写 Fortran 无格式文件 2022-01-01
- 在 Celery 工作人员中捕获 Heroku SIGTERM 以优雅地关 2022-01-01
- pyserial - 可以从线程 a 写入串行端口,是否阻塞从线程 b 读取? 2022-01-01
- 使用生成器和迭代器时 Python 多循环失败 2022-01-01
- 尝试制作WhatsApp机器人 2022-01-01
- 与常规 dict 相比,Python manager.dict() 非常慢 2022-01-01
- Discord.py 缺少必需的参数 2022-01-01
- numpy float:比算术运算中内置的慢 10 倍? 2022-01-01
