node通过pyinstaller 批量打包.py脚本为.exe文件

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 即可。

已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 技术黑板 设计师:CSDN官方博客 返回首页