step1:建立playwright python虚拟环境
新建虚拟环境可以在使用pyinstaller 打包时只打包环境内用到的相关包,减少打包的程序大小。
conda create -n py_venv_wright python==3.7
conda activate py_venv_wright
注意:如果activate后 pip list 仍然是全局的包的列表,需要在运行pip 命令和 Pyinstaller 、playwright 命令时 指定 虚拟环境下该命令的位置 + 命令参数 如:
d:\envs\venv_pyw\Scripts\pip.exe install pywin32
d:\envs\venv_pyw\Scripts\pip.exe install playwright
d:\envs\venv_pyw\Scripts\playwright.exe install chromium
d:\envs\venv_pyw\Scripts\pyinstaller.exe -F -i logo.ico xxxyyy.py --exclude-module pydoc
step2: 安装需要的包
pip install playwright
pip install pyinstaller
pip install pywin32
pip install other_packages_you_need
step3 打包
准备好写好的python 程序代码,在cmd 命令窗口下运行,准备好logo.ico文件(应用程序图标文件也可以不用,可以在这个网站上上传图片转换为ico文件: https://www.bitbug.net/),
set PLAYWRIGHT_BROWSERS_PATH=0
playwright install chromium
# 如果报错 PyInstaller.exceptions.ImportErrorWhenRunningHook: Failed to import module __PyInstaller_hooks_0_pydoc required by hook for module
# 则加 --exclude-module pydoc
# 如: pyinstaller -F -i logo.ico xxxyyy.py --exclude-module pydoc
# 带图标的
# pyinstaller -F -i logo.ico xxxyyy.py
# 不带图标的
pyinstaller -F xxxyyy.py
参考链接:
- https://playwright.dev/python/docs/intro#pyinstaller