electron python_在Electron应用中运行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.

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

})

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?

解决方案

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'])

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值