PyVirtualDisplay is a Python wrapper for Xvfb, Xephyr, and Xvnc programs. It enables running graphical applications or tests in a headless environment by creating a virtual display, making it suitable for CI/CD pipelines and automated GUI testing. The library is currently at version 3.0 and supports Python versions 3.6 through 3.12.
pip install pyvirtualdisplayVerified import paths — ran on the pinned version, not inferred.
This example demonstrates how to start and stop a virtual display using a context manager. It sets up an Xvfb backend with a specific resolution and ensures the display is properly managed. The `visible=0` argument ensures the display runs in headless mode.
Review the official documentation and migrate code to use the updated API, particularly for `Display` constructor arguments and method calls. Pin `pyvirtualdisplay` to an older version (e.g., `==0.2.5`) if immediate migration is not possible.
Install the necessary X server packages on your system (e.g., `sudo apt-get install xvfb` on Debian/Ubuntu, `brew install xquartz` for Xvfb on MacOS). Ensure the chosen backend is in your system's PATH.
Pass `manage_global_env=False` to the `Display` constructor. You will then need to manually manage the `DISPLAY` environment variable for each thread/process if it needs to interact with specific displays.
Address the underlying cause of slow X server startup (e.g., system resources, misconfiguration). For older versions (pre-1.0), it might be possible to remove `xdpyinfo` (macOS workaround) or ensure proper permissions for `/tmp/.X11-unix`.
For Windows, use Windows Subsystem for Linux (WSL2) with an X server or utilize headless modes provided directly by web browsers (e.g., Chrome/Firefox headless).
Install Xvfb on your operating system. For Debian/Ubuntu-based systems, use: `sudo apt-get install xvfb`
Ensure that the graphical application is started within the context of the active pyvirtualdisplay display, often by using a 'with' statement for the Display object, which automatically sets the DISPLAY environment variable.
Increase the `timeout` parameter when initializing the `Display` object to allow more time for the virtual display to start, e.g., `display = Display(visible=0, size=(800, 600), timeout=30)`.
Change the import statement to `from pyvirtualdisplay import Display` or `import pyvirtualdisplay`.