使用PyInstaller打包Python+Selenium为exe文件
前期准备
- Python与selenium环境
- 已完成编码的xxx.py文件
- 与浏览器相对应的驱动chromedriver.exe \ geckodriver.exe \ IEDriverServer.exe
安装PyInstaller
用命令行安装PyInstaller:
pip install pyinstaller
开始打包
- 在xxx.py文件目录下执行pyinstaller xxx.py进行打包, 打包成功后可在dist文件夹下找到同名xxx.exe文件
- 把浏览器驱动文件复制到dist文件夹下
pyinstaller -F xxx.py # -F 生成单个可执行文件
pyinstaller -w xxx.py # -w 启动可执行文件后不显示命令行窗口
pyinstaller -i xxx.ico xxx.py # -i 加上图标打包 注: 打包成功后移动xxx.exe文件到新的文件夹可显示图标
打包时遇到的问题
Fatal error: PyInstaller does not include a pre-compiled bootloader for your
platform. For more details and instructions how to build the bootloader see
https://pyinstaller.readthedocs.io/en/stable/bootloader-building.html
- 卸载之前的PyInstaller
pip uninstall pyinstaller
- 下载并解压缩源码包, 并构建引导加载程序:
cd bootloader
python ./waf all
- 安装PyInstaller:
cd…
python setup.py install
环境
Windows 10
Python 3.9.1
PyInstaller 4.2