[Python] Playwright Persistent Context with msedge.exe Fails in PyInstaller-Compiled...

Discussão em 'Python' iniciado por Stack, Março 10, 2025.

  1. Stack

    Stack Membro Participativo

    After compiling my script with PyInstaller, the executable fails to launch a persistent Playwright browser context using Microsoft Edge, throwing a TargetClosedError. The same code runs fine as a plain Python script in my virtual environment. The issue is almost consistent, but sometimes worked during debugging (adding messageboxes, pauses, etc.) but never worked consistently, suggesting a possible race condition. The method using executable_path and launch_persistent_context worked previously (last used successfully February 2025, version compiled back in April 2024), and no major changes should have been made to my environment, although Microsoft Edge may have updated recently and I compiled this on a new pc (same Python version, 3.12). Some modules in my environment are for features in the full script, that are not used in the minimal example, but included it for transparency.

    Minimal Reproducible Example (test.py):

    import os
    import asyncio
    from playwright.async_api import async_playwright

    async def main():
    executable_path = r"C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe"
    base_dir = os.getcwd()
    user_data_dir = os.path.join(base_dir, "User Data")
    os.makedirs(user_data_dir, exist_ok=True)

    async with async_playwright() as p:
    context = await p.chromium.launch_persistent_context(
    user_data_dir=user_data_dir,
    headless=False,
    executable_path=executable_path,
    )
    page = await context.new_page()
    await page.goto("https://google.com")
    # Wait a few seconds to observe the browser window
    await asyncio.sleep(5)
    print("Page title:", await page.title())
    await context.close()

    if __name__ == "__main__":
    asyncio.run(main())


    Traceback When Running the Compiled Executable:

    Traceback (most recent call last):
    File "test.py", line 24, in <module>
    File "asyncio\runners.py", line 194, in run
    File "asyncio\runners.py", line 118, in run
    File "asyncio\base_events.py", line 686, in run_until_complete
    File "test.py", line 11, in main
    File "playwright\async_api\_generated.py", line 14681, in launch_persistent_context
    File "playwright\_impl\_browser_type.py", line 159, in launch_persistent_context
    File "playwright\_impl\_connection.py", line 61, in send
    File "playwright\_impl\_connection.py", line 528, in wrap_api_call
    playwright._impl._errors.TargetClosedError: BrowserType.launch_persistent_context: Target page, context or browser has been closed
    Browser logs:

    <launching> C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe --disable-field-trial-config --disable-background-networking --disable-background-timer-throttling --disable-backgrounding-occluded-windows --disable-back-forward-cache --disable-breakpad --disable-client-side-phishing-detection --disable-component-extensions-with-background-pages --disable-component-update --no-default-browser-check --disable-default-apps --disable-dev-shm-usage --disable-extensions --disable-features=ImprovedCookieControls,LazyFrameLoading,GlobalMediaControls,DestroyProfileOnBrowserClose,MediaRouter,DialMediaRouteProvider,AcceptCHFrame,AutoExpandDetailsElement,CertificateTransparencyComponentUpdater,AvoidUnnecessaryBeforeUnloadCheckSync,Translate,HttpsUpgrades,PaintHolding,ThirdPartyStoragePartitioning,LensOverlay,PlzDedicatedWorker --allow-pre-commit-input --disable-hang-monitor --disable-ipc-flooding-protection --disable-popup-blocking --disable-prompt-on-repost --disable-renderer-backgrounding --force-color-profile=srgb --metrics-recording-only --no-first-run --enable-automation --password-store=basic --use-mock-keychain --no-service-autorun --export-tagged-pdf --disable-search-engine-choice-screen --unsafely-disable-devtools-self-xss-warnings --no-sandbox --user-data-dir=C:\Users\[username]\Desktop\Playwright_Test\dist\test\User Data --remote-debugging-pipe about:blank
    <launched> pid=21472
    [pid=21472] <process did exit: exitCode=0, signal=null>
    [pid=21472] starting temporary directories cleanup
    Call log:
    - <launching> C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe --disable-field-trial-config --disable-background-networking --disable-background-timer-throttling --disable-backgrounding-occluded-windows --disable-back-forward-cache --disable-breakpad --disable-client-side-phishing-detection --disable-component-extensions-with-background-pages --disable-component-update --no-default-browser-check --disable-default-apps --disable-dev-shm-usage --disable-extensions --disable-features=ImprovedCookieControls,LazyFrameLoading,GlobalMediaControls,DestroyProfileOnBrowserClose,MediaRouter,DialMediaRouteProvider,AcceptCHFrame,AutoExpandDetailsElement,CertificateTransparencyComponentUpdater,AvoidUnnecessaryBeforeUnloadCheckSync,Translate,HttpsUpgrades,PaintHolding,ThirdPartyStoragePartitioning,LensOverlay,PlzDedicatedWorker --allow-pre-commit-input --disable-hang-monitor --disable-ipc-flooding-protection --disable-popup-blocking --disable-prompt-on-repost --disable-renderer-backgrounding --force-color-profile=srgb --metrics-recording-only --no-first-run --enable-automation --password-store=basic --use-mock-keychain --no-service-autorun --export-tagged-pdf --disable-search-engine-choice-screen --unsafely-disable-devtools-self-xss-warnings --no-sandbox --user-data-dir=C:\Users\[username]\Desktop\Playwright_Test\dist\test\User Data --remote-debugging-pipe about:blank
    - - <launched> pid=21472
    - - [pid=21472] <process did exit: exitCode=0, signal=null>
    - - [pid=21472] starting temporary directories cleanup


    Compile Script (build.bat):

    call venv\scripts\activate
    set PLAYWRIGHT_BROWSERS_PATH=0
    pyinstaller --noconsole test.py


    Compile Output:

    207 INFO: PyInstaller: 6.11.1, contrib hooks: 2024.11
    207 INFO: Python: 3.12.8
    242 INFO: Platform: Windows-11-10.0.22631-SP0
    242 INFO: Python environment: C:\Program Files\Python312
    244 INFO: wrote C:\Users\[username]\Desktop\Playwright_Test\test.spec
    247 INFO: Module search paths (PYTHONPATH):
    ['C:\\Program Files\\Python312\\Scripts\\pyinstaller.exe',
    'C:\\Program Files\\Python312\\python312.zip',
    'C:\\Program Files\\Python312\\DLLs',
    'C:\\Program Files\\Python312\\Lib',
    'C:\\Program Files\\Python312',
    'C:\\Users\\[username]\\AppData\\Roaming\\Python\\Python312\\site-packages',
    'C:\\Program Files\\Python312\\Lib\\site-packages',
    'C:\\Program Files\\Python312\\Lib\\site-packages\\win32',
    'C:\\Program Files\\Python312\\Lib\\site-packages\\win32\\lib',
    'C:\\Program Files\\Python312\\Lib\\site-packages\\Pythonwin',
    'C:\\Program Files\\Python312\\Lib\\site-packages\\setuptools\\_vendor',
    'C:\\Users\\[username]\\Desktop\\Playwright_Test']
    717 INFO: checking Analysis
    739 INFO: Building because inputs changed
    739 INFO: Running Analysis Analysis-00.toc
    739 INFO: Target bytecode optimization level: 0
    739 INFO: Initializing module dependency graph...
    742 INFO: Initializing module graph hook caches...
    749 INFO: Analyzing base_library.zip ...
    2152 INFO: Processing standard module hook 'hook-encodings.py' from 'C:\\Program Files\\Python312\\Lib\\site-packages\\PyInstaller\\hooks'
    3239 INFO: Processing standard module hook 'hook-pickle.py' from 'C:\\Program Files\\Python312\\Lib\\site-packages\\PyInstaller\\hooks'
    3710 INFO: Processing standard module hook 'hook-heapq.py' from 'C:\\Program Files\\Python312\\Lib\\site-packages\\PyInstaller\\hooks'
    3921 INFO: Caching module dependency graph...
    3987 INFO: Looking for Python shared library...
    3997 INFO: Using Python shared library: C:\Program Files\Python312\python312.dll
    3997 INFO: Analyzing C:\Users\[username]\Desktop\Playwright_Test\test.py
    4085 INFO: Processing standard module hook 'hook-multiprocessing.util.py' from 'C:\\Program Files\\Python312\\Lib\\site-packages\\PyInstaller\\hooks'
    4166 INFO: Processing standard module hook 'hook-xml.py' from 'C:\\Program Files\\Python312\\Lib\\site-packages\\PyInstaller\\hooks'
    4496 INFO: Processing standard module hook 'hook-playwright.async_api.py' from 'C:\\Program Files\\Python312\\Lib\\site-packages\\playwright\\_impl\\__pyinstaller'
    4859 INFO: Processing standard module hook 'hook-platform.py' from 'C:\\Program Files\\Python312\\Lib\\site-packages\\PyInstaller\\hooks'
    5025 INFO: Processing module hooks (post-graph stage)...
    5143 INFO: Performing binary vs. data reclassification (343 entries)
    5219 INFO: Looking for ctypes DLLs
    5225 INFO: Analyzing run-time hooks ...
    5228 INFO: Including run-time hook 'pyi_rth_inspect.py' from 'C:\\Program Files\\Python312\\Lib\\site-packages\\PyInstaller\\hooks\\rthooks'
    5231 INFO: Including run-time hook 'pyi_rth_pkgutil.py' from 'C:\\Program Files\\Python312\\Lib\\site-packages\\PyInstaller\\hooks\\rthooks'
    5231 INFO: Including run-time hook 'pyi_rth_multiprocessing.py' from 'C:\\Program Files\\Python312\\Lib\\site-packages\\PyInstaller\\hooks\\rthooks'
    5239 INFO: Looking for dynamic libraries
    5465 INFO: Extra DLL search directories (AddDllDirectory): []
    5471 INFO: Extra DLL search directories (PATH): []
    5714 INFO: Warnings written to C:\Users\[username]\Desktop\Playwright_Test\build\test\warn-test.txt
    5729 INFO: Graph cross-reference written to C:\Users\[username]\Desktop\Playwright_Test\build\test\xref-test.html
    5753 INFO: checking PYZ
    5774 INFO: Building because name changed
    5774 INFO: Building PYZ (ZlibArchive) C:\Users\[username]\Desktop\Playwright_Test\build\test\PYZ-00.pyz
    6099 INFO: Building PYZ (ZlibArchive) C:\Users\[username]\Desktop\Playwright_Test\build\test\PYZ-00.pyz completed successfully.
    6105 INFO: checking PKG
    6114 INFO: Building because name changed
    6114 INFO: Building PKG (CArchive) test.pkg
    6132 INFO: Building PKG (CArchive) test.pkg completed successfully.
    6136 INFO: Bootloader C:\Program Files\Python312\Lib\site-packages\PyInstaller\bootloader\Windows-64bit-intel\runw.exe
    6136 INFO: checking EXE
    6175 INFO: Building because name changed
    6179 INFO: Building EXE from EXE-00.toc
    6179 INFO: Copying bootloader EXE to C:\Users\[username]\Desktop\Playwright_Test\build\test\test.exe
    6325 WARNING: Execution of 'remove_all_resources' failed on attempt #1 / 20: error(110, 'EndUpdateResourceW', 'The system cannot open the device or file specified.'). Retrying in 0.05 second(s)...
    6380 INFO: Copying icon to EXE
    6397 INFO: Copying 0 resources to EXE
    6397 INFO: Embedding manifest in EXE
    6538 INFO: Appending PKG archive to EXE
    6541 WARNING: Execution of '_append_data_to_exe' failed on attempt #1 / 20: PermissionError(13, 'Permission denied'). Retrying in 0.05 second(s)...
    6597 WARNING: Execution of '_append_data_to_exe' failed on attempt #2 / 20: PermissionError(13, 'Permission denied'). Retrying in 0.06 second(s)...
    6654 WARNING: Execution of '_append_data_to_exe' failed on attempt #3 / 20: PermissionError(13, 'Permission denied'). Retrying in 0.06 second(s)...
    6714 WARNING: Execution of '_append_data_to_exe' failed on attempt #4 / 20: PermissionError(13, 'Permission denied'). Retrying in 0.06 second(s)...
    6954 INFO: Fixing EXE headers
    7658 INFO: Building EXE from EXE-00.toc completed successfully.
    7661 INFO: checking COLLECT
    WARNING: The output directory "C:\Users\[username]\Desktop\Playwright_Test\dist\test" and ALL ITS CONTENTS will be REMOVED! Continue? (y/N)y
    On your own risk, you can use the option `--noconfirm` to get rid of this question.
    10946 INFO: Removing dir C:\Users\[username]\Desktop\Playwright_Test\dist\test
    11021 INFO: Building COLLECT COLLECT-00.toc
    11682 INFO: Building COLLECT COLLECT-00.toc completed successfully.


    Requirements (requirements.txt):

    et_xmlfile==2.0.0
    greenlet==3.1.1
    numpy==2.2.1
    openpyxl==3.1.5
    pandas==2.2.3
    playwright==1.49.1
    pyee==12.0.0
    PySide6==6.8.1.1
    PySide6_Addons==6.8.1.1
    PySide6_Essentials==6.8.1.1
    python-dateutil==2.9.0.post0
    pytz==2024.2
    shiboken6==6.8.1.1
    six==1.17.0
    typing_extensions==4.12.2
    tzdata==2024.2

    Continue reading...

Compartilhe esta Página