电子生成器未捆绑python文件 [英] electron-builder is not bundling the python files
问题描述
这是我的目录结构,其中 index.html
包含 renderer.js
。
从 renderer调用python脚本
通过 python-shell 。 visitor.py
和 download.py
.js
捆绑后,就找不到python脚本
This is my directory structure where renderer.js
is included by index.html
.
The python scripts visitor.py
and download.py
are called from renderer.js
via python-shell.
Once I bundle, it is not able to find the python scripts
|_ index.html
|_ styles.css
|_ main.js
|_ package.json
|_ dist/
|_ node_modules/
|_ renderer.js
|_ visitor.py
|_ download.py
我尝试将所有内容放入文件:
,然后运行 build>下的
文件 package.json
中的[...] npm run dist
。
我还尝试将python文件明确复制到 dist
文件夹,然后运行 npm run dist
。
没有任何作用。
I tried putting everything in files: [...]
in package.json
under build > files
and then ran npm run dist
.
I also tried copying python files to dist
folder explicitly and then ran npm run dist
.
None are working.
/Application/test.app/Contents/Resources/app.asar/remderer.js:226
错误:python:无法打开文件'visitor.py':[错误2]没有这样的文件或
目录
/Application/test.app/Contents/Resources/app.asar/remderer.js:226 Error: python: can't open file 'visitor.py': [Error 2] No such file or directory
这是我的package.json
This is my package.json
{
"name": "test",
"version": "1.0.0",
"description": "",
"main": "main.js",
"scripts": {
"start": "electron .",
"pack": "build --dir",
"dist": "build"
},
"author": "",
"license": "ISC",
"build": {
"appId": "com.example.app",
"files": [
"dist/",
"node_modules/",
"index.html",
"main.js",
"package.json",
"renderer.js",
"styles.css",
"visitor.py",
"download.py"
],
"dmg": {
"contents": [
{
"x": 110,
"y": 150
},
{
"x": 240,
"y": 150,
"type": "link",
"path": "/Applications"
}
]
},
"linux": {
"target": [
"AppImage",
"deb"
]
},
"win": {
"target": "squirrel",
"icon": "build/icon.ico"
}
},
"dependencies": {
"csv-parse": "^2.5.0",
"electron-css": "^0.6.0",
"npm": "^6.1.0",
"python-shell": "^0.5.0",
},
"devDependencies": {
"electron": "^2.0.3",
"electron-builder": "^20.19.1"
}
}
PS:
这是我正在谈论的电子制造商
https://github.com/electron-userland/electron-builder
推荐答案
请确保不要使用ypo
/Application/test.app/Contents/Resources/app.asar/remderer.js:226错误:python:无法打开文件'visitor.py':[错误2]没有这样的文件或目录
是 remderer.js ,而其他地方是 renderer.js ,所以请确保不是您的错字。如果是,请更正。
Please makesure not your typo
/Application/test.app/Contents/Resources/app.asar/remderer.js:226 Error: python: can't open file 'visitor.py': [Error 2] No such file or directory
is remderer.js, but other place is renderer.js, so please makesure is NOT your typo. If is, correct it.
实际上 electron-builder
IS 捆绑了您的python文件,但是由于 asar
,您的 python-shell
找不到您的python文件,因此导致错误。
Actually electron-builder
IS bundled your python file, but due to asar
, your python-shell
can NOT find your python file, so cause the error.
最简单,但官方不推荐:禁用asar
Easiest but not recommend by official: disable asar
更改您的 package.json
到:
{
...
"build": {
"appId": "com.example.app",
...
"asar": false,
然后在 renderer.js
中,其中包含 python-shell
代码,也许像这样:
then in your renderer.js
, which contain python-shell
code, maybe like this:
import {PythonShell} from 'python-shell';
PythonShell.run('visitor.py', null, function (err) {
if (err) throw err;
console.log('finished');
});
现在应该工作。
禁用asar后,所有相关文件路径都不包含asar,请变成:
after disable asar, the all related file path is not contain asar, become this:
-
/Application/test.app/Contents/Resources/app/visitor.py
-
/Application/test.app/Contents/Resources/app/renderer.js
/Application/test.app/Contents/Resources/app/visitor.py
/Application/test.app/Contents/Resources/app/renderer.js
即, .app
文件结构为:
|_ test.app
|_ Contents
|_ Resources
|_ app
|_ styles.css
|_ main.js
|_ package.json
|_ dist/
|_ node_modules/
|_ renderer.js
|_ visitor.py
|_ download.py
...
解决方案2:
保持启用asar,将其他文件放入解包
:
将您 package.json
更改为:
{
...
"build": {
"appId": "com.example.app",
...
"asar": true,
"asarUnpack": [
"visitor.py",
"download.py"
"renderer.js"
],
打包的 .app
文件结构为:
|_ test.app
|_ Contents
|_ Resources
|_ app.asar # a single compressed binary file
|_ app.asar.unpacked # a folder/directory, contain unpacked origin files
|_ visitor.py
|_ download.py
|_ renderer.js
您的 renderer.js
,可能不需要更改,并且应该可以正常工作。
your renderer.js
, maybe NOT need change, and should working.
有关<$ c $的详细信息c> asarUnpack ,请参阅官方文档:每个平台选项可覆盖
more details about asarUnpack
please refer official doc: Overridable per Platform Options
PS:其他一些asar及其相关尝试,可以参考我的中文帖子:【已解决】 mac中PyInstaller打包后的二进制文件在electron-builder打包后app中有些无法通过child_process的execFile运行
PS: some other asar and related trying, can refer my Chinese post: 【已解决】mac中PyInstaller打包后的二进制文件在electron-builder打包后app中有些无法通过child_process的execFile运行
这篇关于电子生成器未捆绑python文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!