在Electron应用中运行python脚本 [英] Run python script in Electron app
问题描述
我有一个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 :
"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屋!