Run python script in Electron app

眉间皱痕 提交于 2019-12-02 17:21:54

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

I've solved my problem. I'll explain for possible future readers with the same problem.

Using electron builder, there are some options possible for not to package the application source code into an electron file. These options are:

asar

Whether to package the application’s source code into an archive, using Electron’s archive format. Defaults to true. Node modules, that must be unpacked, will be detected automatically, you don’t need to explicitly set asarUnpack - please file issue if this doesn’t work.

Or you can pass object of asar options.

asarUnpack

A glob patterns relative to the app directory, which specifies which files to unpack when creating the asar archive.

DOCS

Setting asar to false solves the issue, but it is not recommended by electron-builder.

So including all the files I need to unpack in a folder, and using "asarUnpack" : "my-folder/*" is the right way. Now all the files unpacked are available in /resources/app.asar.unpacked/my-folder

Another thing to take into account is that using the path './ResolvePosition.py' is going to look at the root folder of my electron project, not the path where my NodeJS file is located, I need to use :

let python = spawn('python', [path.join(__dirname, '../app.asar.unpacked/my-folder', 'ResolvePosition.py'), obsFilePath, navFilePath])
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!