python编译成exe (使用pyinstaller)

    xiaoxiao2022-07-07  179

    pyinstaller

    pyinstaller其实就是把python解析器和你自己的脚本打包成一个可执行的文件。好处就是在运行者的机器上不用安装python和你的脚本依赖的库。pyinstaller打包的执行文件,只能在和打包机器系统同样的环境下。也就是说,不具备可移植性,若需要在不同系统上运行,就必须针对该平台进行打包。

    pyinstaller的安装

    pip install pyinstaller

    pyinstaller的使用

    基本语法: pyinstaller options myscript.py

    常用的可选参数如下:

    --onefile 将结果打包成一个可执行文件 --onedir 将所有结果打包到一个文件夹中,该文件夹包括一个可执行文件和可执行文件执行时需要的依赖文件(默认) --paths=DIR 设置导入路径 --distpath=DIR 设置将打包的结果文件放置的路径 --specpath=DIR 设置将spec文件放置的路径 --windowed 使用windows子系统执行,不会打开命令行(只对windows有效) --nowindowed 使用控制台子系统执行(默认)(只对windows有效) --icon=<FILE.ICO> 将file.ico添加为可执行文件的资源(只对windows有效)

    pyinstaller --onefile test.py

    会在当前文件下形成build文件夹、dist文件夹和.spec文件。 dist里就是test.exe可执行文件


    AttributeError: module ‘enum’ has no attribute 'IntFlag’的解决

    打出exe后,运行提示 AttributeError: module 'enum' has no attribute 'IntFlag' 原因: This is likely caused by the package enum34. Since python 3.4 there’s a standard library enum module, so you should uninstall enum34, which is no longer compatible with the enum in the standard library since enum.IntFlag was added in python 3.6. 解决办法: pip uninstall enum34 #卸载enum34 然后重新执行 pyinstaller 生成exe

    最新回复(0)