1. package.json
"scripts": {
"postinstall": "node ./static/python/index.js"
}
2. static/python/index.js,批量把 static/python/py/下的所有.py脚本打包到 static/python/exe/下 打包为.exe.
const requireContent = require("require-context");
const chalk = require("chalk");
const path = require("path");
const pyPath = path.join(__dirname, "./py"); // 从py文件夹下的.py文件
const exePath = path.join(__dirname, "./exe"); //打包到exe文件夹下 .exe
const testsContent = requireContent(pyPath, false, /\.py$/); // 匹配py文件夹下的.py文件
const exec = require("child_process").exec;
const keysList = testsContent.keys();
for (let i = 0; i < keysList.length; i++){
const item = keysList[i];
// pyinstaller -F ./xx.py --distpath./exe 把xx.py打包成exe/xx.exe
exec(`pyinstaller -F ${pyPath}/${item} --distpath ${exePath}`,{encoding:"utf8"}, (err, stdout, sterr) => {
if (err) {
console.log(chalk.red("×") + ` python打包${item}失败:${err}`);
} else {
console.log(chalk.green("√") + ` python打包${item}成功`);
}
})
}
3. 安装pyinstaller :pip install pyinstaller
4. npm run postinstall 即可。