推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
Juszoe
V2EX  ›  Python

Pyinstaller 打包无头浏览器,弹出 cmd 窗口的问题

  •  
  •   Juszoe · Oct 6, 2022 · 2916 views
    This topic created in 1339 days ago, the information mentioned may be changed or developed.

    用 PyQt(PySide)和 Playwright(无头浏览器)写了个小软件,headless 也设置成True了,直接用 python 运行是不会弹出任何额外的 cmd 窗口的。

    但是当我用如下命令打包:

    pyinstaller -F -w ui.py --key xxxxx --hidden-import "PySide6.QtGui" -i asset/favicon.ico
    

    实际效果:主程序的 cmd 窗口是没有了,但是每启动一个无头浏览器,会弹出一个 cmd 窗口(没有任何内容)。

    初步猜测是因为启动了一个浏览器进程,所以 pyinstaller 没有 hook 住,查到文档说要在主程序开始前调用multiprocessing.freeze_support(),但我尝试过也没有效果。

    请问各位大佬们有遇到过类似的问题吗?

    3 replies    2022-10-06 16:54:56 +08:00
    Ct5T66PVR1bW7b2z
        1
    Ct5T66PVR1bW7b2z  
       Oct 6, 2022   ❤️ 2
    修改源码:Lib\site-packages\selenium\webdriver\common\services.py
    查找 creationflags
    将"creationflags=0" 改为 "creationflags=134217728" 即可
    Juszoe
        2
    Juszoe  
    OP
       Oct 6, 2022
    @Abbeyok #1 大佬强啊
    虽然我用的不是 selenium ,是 playwright ,但是根据您提供的线索,找到对应代码:

    if sys.platform == "win32" and sys.stdout is None:
    creationflags = subprocess.CREATE_NO_WINDOW # 其实这里是 134217728

    所以只要在启动浏览器前执行 sys.stdout = None 就可以了,不用修改库代码
    Juszoe
        3
    Juszoe  
    OP
       Oct 6, 2022
    附个源码位置
    \Lib\site-packages\playwright\_impl\_transport.py
    PipeTransport.connect 方法
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1001 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 37ms · UTC 23:22 · PVG 07:22 · LAX 16:22 · JFK 19:22
    ♥ Do have faith in what you're doing.