为 Linux 系统打包我的应用程序后,用于执行某些后台任务的 python 脚本无法执行,可能是因为它未正确打包。我现在已经浏览了几个线程,偶然发现了 ASAR、extraResources 等,但我并没有真正让它工作。
我的文件夹结构如下
- App.Root
|-public
| -background.html
|
|-scripts
-python.py
这是我的background.html的内容:
<script>
const { PythonShell } = require('python-shell');
const { ipcRenderer } = require('electron');
const path = require('path');
let pyshell
ipcRenderer.on('START_PROCESSING', (event, args) => {
console.log("Loading Python")
const { data } = args;
pyshell = new PythonShell(path.join(__dirname, '/../scripts/python.py'), {
pythonPath: 'python3',
pythonOptions:['-u'],
args: [data]
});
pyshell.on('message', function (results) {
ipcRenderer.send('MESSAGE_FROM_BACKGROUND', { message: results });
});
});
ipcRenderer.on('QUIT_PYTHON', (event, args) => {
pyshell.kill();
ipcRenderer.send('CLOSE_BACKGROUND');
});
ipcRenderer.send('BACKGROUND_READY');
我已经尝试过包含这样的脚本文件夹,我认为它有效,因为它出现在“dist”文件夹中,但不知道从这里去哪里:
"extraResources": [
{
"from": "scripts",
"to": "scripts"
}
我知道这是路径以及 Electron 如何打包应用程序的问题,但由于我对这一切都是新手,所以我感到有点不知所措。人们发布了一堆对我不起作用的不同解决方案,或者问题从未得到解决。
如果熟悉打包 Electron 应用程序的人可以插话,我会很高兴。
//编辑:
这是我当前的构建配置:
"build": {
"files": [
"build/**/*",
"node_modules/**/*"
],
"asarUnpack":"./scripts/**",
"extraResources": "./scripts/**",
"publish": {
"provider": "github",
"repo": "test",
"owner": "Test"
}
},
我也改变了:
pyshell = new PythonShell(path.join(__dirname, '/../scripts/python.py')
致:
pyshell = new PythonShell(path.join(process.resourcesPath, '/scripts/python.py')
因为它显然是错误的。
最佳答案
我还在我的 Electron 应用程序中使用 Python 脚本。
我需要查看您的整个 build {} 配置才能看得更清楚,但我认为问题是因为您的 asar 未正确包含您的 ./script
文件夹
在我的 Electron 构建器配置中我有这个
"asarUnpack": "./scripts/**",
"extraResources": ["./scripts/**", "./scripts/database/**"], //Note that I also include a folder named database inside my ./script folder
使用此行,我的文件夹 ./scripts
已正确包含
编辑:// 我的 block 包含一个将我登录到我的应用程序中的脚本:
ipcMain.on(`login`, async (event, args) => {
let command = ``;
if (process.platform === `linux`) {
command = await exec(`./scripts/login ${args.user} ${args.password}`);
}
command.stdout.on(`data`, (data) => {
// do something when scripts runs
command.on(`exit`, (code) => {
console.log(code);
});
关于python - 在打包的 Electron 应用程序中包含 python 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73520412/