你是否有这种烦恼?
我会发现每次搞这些都要耗费一大堆时间,于是想到以前也有编译打包可执行文件,如Windows exe文件直接执行。
PyInstaller是一个用于将Python程序转换为独立的可执行文件的工具。使用PyInstaller,您可以将Python程序编译打包为单个可执行文件,该文件可以在没有Python解释器的计算机上运行,但是它不是交叉编译器,既不能夸操作系统编译打包,比如Windows只能编译打包为exe,不能编译为Linux、mac等操作系统。
官方文档:https://pyinstaller.org/en/stable
以下是使用PyInstaller将Python程序编译打包为可执行文件的基本步骤:
1、确保已经安装了PyInstaller。如果没有安装,可以使用以下命令进行安装:
pip install pyinstaller
2、打开终端或命令提示符,并导航到包含您的Python程序的目录。
3、运行以下命令来将Python程序打包为可执行文件:
pyinstaller your_script.py
其中your_script.py是您的Python程序的文件名。
4、PyInstaller将在当前目录中创建一个名为dist的文件夹,其中包含生成的可执行文件。在Windows上,可执行文件将具有.exe扩展名,而在Linux和Mac上则没有扩展名。

5、pyinstaller 命令部分简介
pyinstaller --help
我最常用的为:
pyinstaller -F -i xxx.ico xxx.py
1、script.py
while 1:
word = input("请输入需要打印的字符:")
print(word)
if word.lower() == 'q':
break
2、ico
可以自行找一个
3、运行命令
pyinstaller -F -i ./script.ico script.py

4、运行可执行文件 Windows
由于是Windows下编译打包,则为exe,直接双击运行。

5、运行可执行文件 Linux
Linux打包命令也是一致的。
./script

pyinstaller -F -i ./desc/build.ico ppl.py
依赖一般都会打入,但是其中也有入到坑,比如 pytest
import pytest
from allure_pytest import plugin as allure_plugin
# pytest.main(argv)
pytest.main(argv, plugins=[allure_plugin]) # todo use package
当我编译打包时需要将 allure_plugin 依赖打入,但是在ide中运行又报错,于是只能改为:
import pytest
from allure_pytest import plugin as allure_plugin
pytest.main(argv)
# pytest.main(argv, plugins=[allure_plugin]) # todo use package
pytest.main(argv) 才得以解决,报错如下:
ValueError: Plugin already registered: allure_pytest=
1、打包后执行exe发现以下错误:
Traceback (most recent call last):
File "" , line 1, in <module>
OSError: [WinError 6] 句柄无效。
后看到是loguru日志模块报错:
也看到了此 issues 但是没有实际解决。
2、于是在运行时添加一个自定义参数表示日志是否用 loguru
默认是用,如果添加参数 --colour=0 则表示不使用loguru。
部分代码改动如下:类 PplLog 打印 print 分别有使用到loguru的方法一致,这样代码改动最小了。

3、打包测试
发现正常了,应该就是这个原因 issues 但貌似没有看到更好的解决办法,只能先这样了。

优点:
缺点:
怎么总结下来是弊大于利?没有最好的语言或工具,适合的才是最好。
所以按需择用!
