Raja's Exocortex

Playwright Portable

How to install a portable version of Playwright for Python on Windows 11 for RPA.

Playwright, browsers, ffmpeg and all dependencies are installed in a custom path and do not interefere with other Python or browser installations.

1. Create a Windows Sandbox

Windows Sandbox can be used to create ephemeral environments for installing and packaging Playwright. Install the Windows Sandbox feature and follow the below steps inside the Sandbox.

2. Create a Virtual Drive Using subst

The subst command on Windows can be used to mount any folder as a drive letter.

Using subst create a new drive letter (eg P:) and install python with all modules in this drive.

mkdir c:\temp
subst p: c:\temp

3. Install Python

Download the latest version of Python and perform a custom install. During the installation, set the install path to P:\Python.

4. Install Playwright

@rem upgrade pip
P:\python\python.exe -m pip install --upgrade pip

@rem install playwright
P:\python\Scripts>pip install playwright
P:\python\Scripts>pip install pytest-playwright

@rem set custom install path for browsers and ffmpeg
set PLAYWRIGHT_BROWSERS_PATH=p:\pw-browsers

@rem install playwright deps, browsers and ffmpeg
P:\python\Scripts>playwright.exe install

5. Testing

# Filename: P:\scripts\chrome-google.py
# Run as: P:\python\python.exe P:\scripts\chrome-google.py

from playwright.sync_api import sync_playwright

# Set the environment variable for Playwright browsers path
os.environ['PLAYWRIGHT_BROWSERS_PATH'] = 'p:\\pw-browsers'

# Start Playwright
with sync_playwright() as p:
    # Launch Chrome browser (not headless)
    browser = p.chromium.launch(headless=False)
    # Open a new browser context
    context = browser.new_context()
    # Open a new page
    page = context.new_page()
    # Navigate to Google
    page.goto("https://www.google.com")

    # Keep the browser open until manually closed
    input("Press Enter to close the browser...")

    # Close the context and browser
    context.close()
    browser.close()

6. Packaging

Zip the P: drive contents as playwright.zip and copy it out of the Sandbox.

7. Deploying

To run Playwright on any other Win11 computer:

Extract playwright.zip and subst P: <PLAYWRIGHT_EXTRACTED_FOLDER> to mount the folder as P:.

Run the chrome-google.py test script from the previous step.

set PLAYWRIGHT_BROWSERS_PATH=P:\pw-browsers
P:\python\python.exe P:\scripts\chrome-google.py

To clean up, run subst P: /D to remove the virtual drive.