PyObjC Virtualization Framework
pyobjc-framework-virtualization provides Python wrappers for Apple's Virtualization framework on macOS. It is part of the PyObjC project, a bidirectional bridge enabling Python scripts to interact with Objective-C libraries, including macOS Cocoa frameworks. The current version is 12.1, and it maintains an active release cadence, typically aligning with macOS SDK updates and Python version support.
Warnings
- breaking PyObjC frequently drops support for older Python versions. PyObjC 12.0 dropped support for Python 3.9, and PyObjC 11.0 dropped Python 3.8. Ensure your Python version is compatible with the PyObjC version you are installing.
- breaking PyObjC 11.1 changed how Objective-C initializer methods (those in the `init` family) are modeled, now correctly reflecting that they 'steal' a reference to `self` and return a new one, as per clang's Automatic Reference Counting (ARC) documentation. This affects object lifecycle and reference counting.
- gotcha Actual creation and running of `VZVirtualMachine` instances typically requires your application to have the `com.apple.security.virtualization` entitlement. Without this, operations like `start()` may fail with permission errors.
- gotcha PyObjC 10.3 temporarily removed support for calling `__init__` on Python classes that also define a user-implemented `__new__` method, causing breakage in some projects. This was reverted in version 10.3.1. [from prompt]
- gotcha PyObjC is a wrapper for macOS-specific frameworks. Therefore, `pyobjc-framework-virtualization` is only functional on macOS and is not intended for cross-platform use.
Install
-
pip install pyobjc-framework-virtualization
Imports
- Virtualization
import Virtualization
- VZVirtualMachine
from Virtualization import VZVirtualMachine
Quickstart
import Virtualization
import os
def check_virtualization_support():
print(f"Checking macOS Virtualization support...")
if Virtualization.VZVirtualMachine.isSupported():
print("\u2705 macOS Virtualization framework is supported on this machine.")
print(" You can now proceed to create VZVirtualMachineConfiguration objects and manage virtual machines.")
# Further steps would involve creating a VZVirtualMachineConfiguration,
# setting up devices, and booting a guest OS, which is beyond a quickstart.
# Example of a next step (not fully runnable without more config):
# config = Virtualization.VZVirtualizationMachineConfiguration.alloc().init()
# if config.validateWithError_(None):
# print(" Basic VZVirtualMachineConfiguration object could be initialized.")
else:
print("\u274C macOS Virtualization framework is NOT supported on this machine.")
print(" Requires macOS 11 or later and Apple Silicon or Intel with VT-x.")
if __name__ == "__main__":
check_virtualization_support()