Skip to content

pyinstaller does not put browser in exe file build #45

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
harbet333 opened this issue Apr 24, 2025 · 4 comments
Open

pyinstaller does not put browser in exe file build #45

harbet333 opened this issue Apr 24, 2025 · 4 comments
Assignees
Labels
bug Something isn't working good first issue Good for newcomers help wanted Extra attention is needed wontfix This will not be worked on

Comments

@harbet333
Copy link

harbet333 commented Apr 24, 2025

i have a code:

import asyncio
import json
import os
from patchright.async_api import async_playwright

SESSION_FILE = "session.json"
URL = "https://www.twitch.tv/login"  

class BrowserSession:
    async def load_session(self, browser):
        """Load session."""
        if os.path.exists(SESSION_FILE):
            with open(SESSION_FILE, "r") as f:
                storage_state = json.load(f)
            return await browser.new_context(storage_state=storage_state)
        return await browser.new_context()

    async def save_session(self, context):
        """save session."""
        storage = await context.storage_state()
        with open(SESSION_FILE, "w") as f:
            json.dump(storage, f)
        print("sesion saved")

    async def run(self):
        async with async_playwright() as p:
            browser = await p.chromium.launch(headless=False)
            context = await self.load_session(browser)
            page = await context.new_page()
            
            await page.goto(URL)
            print(f"Open page: {URL}")
            print("Press Enter to save session and exit...")
            
            # waiting Enter
            loop = asyncio.get_event_loop()
            await loop.run_in_executor(None, input)
            
            await self.save_session(context)
            await browser.close()

if __name__ == "__main__":
    session = BrowserSession()
    asyncio.run(session.run())

this code works in visual studio code in venv

I try to create an exe file to execute it, but no matter what I enter in the terminal, it does not include the browser in the exe file, and therefore nothing works

I tried:

set PLAYWRIGHT_BROWSERS_PATH=0
playwright install chromium
pyinstaller -F main.py
set PLAYWRIGHT_BROWSERS_PATH=0
patchright install chromium
pyinstaller -F main.py
set PATCHRIGHT_BROWSERS_PATH=0
patchright install chromium
pyinstaller -F main.py
set PATCHRIGHT_BROWSERS_PATH=0
playwright install chromium
pyinstaller -F main.py

None of these methods work. Please , help me , when using the standard playwright in the code, the command from the manual works https://playwright.dev/python/docs/library

set PLAYWRIGHT_BROWSERS_PATH=0
playwright install chromium
pyinstaller -F main.py

but the standard playwright does not allow logging into twitch

@harbet333
Copy link
Author

harbet333 commented Apr 24, 2025

I tried to do this
pyinstaller --clean -F --collect-all "patchright" pathcrightV2.test.py
as it was written here #10

the file size has increased a little, but it still does not import the browser into the exe file

C:\Users\harbet333w\Desktop\scrStrm\patchRight\dist>pathcrightV2.test.exe
Traceback (most recent call last):
  File "pathcrightV2.test.py", line 44, in <module>
  File "asyncio\runners.py", line 44, in run
  File "asyncio\base_events.py", line 646, in run_until_complete
  File "pathcrightV2.test.py", line 27, in run
  File "patchright\async_api\_generated.py", line 14168, in launch
  File "patchright\_impl\_browser_type.py", line 82, in launch
  File "patchright\_impl\_connection.py", line 46, in send
  File "patchright\_impl\_connection.py", line 478, in wrap_api_call
patchright._impl._errors.Error: BrowserType.launch: Executable doesn't exist at C:\Users\HARBET~1\AppData\Local\Temp\_MEI137362\patchright\driver\package\.local-browsers\chromium-1161\chrome-win\chrome.exe

╔════════════════════════════════════════════════════════════╗
║ Looks like Playwright was just installed or updated.       ║
║ Please run the following command to download new browsers: ║
║                                                            ║
║     playwright install                                     ║
║                                                            ║
║ <3 Playwright Team                                         ║
╚════════════════════════════════════════════════════════════╝
[PYI-17596:ERROR] Failed to execute script 'pathcrightV2.test' due to unhandled exception!

@Vinyzu Vinyzu self-assigned this Apr 24, 2025
@Vinyzu Vinyzu added bug Something isn't working good first issue Good for newcomers labels Apr 24, 2025
@harbet333
Copy link
Author

harbet333 commented Apr 26, 2025

I came up with a temporary solution for this problem, maybe it will help someone who faced the same situation. The paths to the files may differ. Run your generated exe via CMD, and look at the path to the browser you need, and also check the path to your Python, and replace it with your own, for me it turned out like this:

see your python folder here

C:\Users\%username%\AppData\Local\Programs\Python\

see your browsers folder here

C:\Users\%username%\AppData\Local\ms-playwright\

for me it turned out like this:

pyinstaller --onefile --add-data="C:\Users\%username%\AppData\Local\Programs\Python\Python310\Lib\site-packages\patchright\driver;patchright/driver" --add-data="C:\Users\%username%\AppData\Local\ms-playwright\chromium-1161:patchright\driver\package\.local-browsers\chromium-1161" main.py

@harbet333 harbet333 mentioned this issue Apr 27, 2025
@Vinyzu Vinyzu marked this as a duplicate of #43 May 4, 2025
@Vinyzu
Copy link
Contributor

Vinyzu commented May 4, 2025

Ive spend some good amount of time trying to figure this out/debug this, but i could not get anything to work.
Im not too familiar with pyinstaller, maybe someone with more knowledge there can figure this out.

Most likely some setup misconfiguration in the Playwright Registry. Contributions welcome!

@Vinyzu Vinyzu added help wanted Extra attention is needed wontfix This will not be worked on labels May 4, 2025
@kaliiiiiiiiii
Copy link

kaliiiiiiiiii commented May 9, 2025

Im not too familiar with pyinstaller, maybe someone with more knowledge there can figure this out.

Probably requires adding a hook over https://pyinstaller.org/en/stable/hooks.html

e.g. ~ https://github.com/pyinstaller/hooksample/blob/master/src/pyi_hooksample/__pyinstaller/hook-pyi_hooksample.py#L45 & https://github.com/pyinstaller/hooksample/blob/a2aabc4372ebf0f9c0f861f2cba2d6053f29c6b4/setup.cfg#L44

To include the browser executables

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good first issue Good for newcomers help wanted Extra attention is needed wontfix This will not be worked on
Projects
None yet
Development

No branches or pull requests

3 participants