[vbox-dev] How to start a VM from Python/COM ?

Alexey Eromenko al4321 at gmail.com
Sun Jan 30 08:29:43 PST 2011

Hi all !

Today I tried to write small python script to start VirtualBox VMs. It
should work from both Windows and Linux hosts.

Host: Windows Vista 32 + VBox 4.0 + SDK + python 2.5 + pywin32

TRY 1:
I wrote according to VBox SDK docs (2.3.1 Python COM API, v4.0
SDKref.PDF): (plus added few imports)

import os,sys
import win32com

vbox = win32com.client.Dispatch("VirtualBox.VirtualBox")
session = win32com.client.Dispatch("VirtualBox.Session")
mach = vbox.findMachine("uuid or name of machine to start")
progress = mach.launchVMProcess(session, "gui", "")


I get this:

Traceback (most recent call last):
  File "E:\Alex-temp\vbox-devel01.py", line 16, in <module>
    vbox = win32com.client.Dispatch("VirtualBox.VirtualBox")
AttributeError: 'module' object has no attribute 'client'

I tried to read source of "vboxshell.py", but it is too hard for me,
and it does not use "VirtualBox.VirtualBox" unlike the SDK example.
I have basic skills in python, but not in VirtualBox-python and not in
COM programming.
Any ideas ?

TRY 2:
This code I copied from "2.3.2 Common Python bindings layer" SDK PDF.

# This code assumes vboxapi.py from VirtualBox distribution
# being in PYTHONPATH, or installed system-wide
from vboxapi import VirtualBoxManager
# This code initializes VirtualBox manager with default style
# and parameters
virtualBoxManager = VirtualBoxManager(None, None)
# Alternatively, one can be more verbose, and initialize
# glue with webservice backend, and provide authentication
# information
virtualBoxManager = VirtualBoxManager("WEBSERVICE",

And here is the error:
  File "E:\Alex-temp\vbox-devel02.py", line 13
SyntaxError: Non-ASCII character '\x92' in file E:\Alex-temp\vbox-devel02.py on
line 13, but no encoding declared; see http://www.python.org/peps/pep-0263.html
for details

I tried to add:
# -*- coding: ascii -*-
..on the first line, but it did not help.
Any ideas ?

TRY 3:
vboxshell.py renders text incorrectly on my Windows host.

I did one command: "help", and got:

vbox> help
Help page:
    ←[94malias←[0m: Control aliases
    ←[94mattachCtr←[0m: Attach storage controller to the VM: attachCtr win Ctr0
    ←[94mattachHdd←[0m: Attach HDD to the VM: attachHdd win /disk.vdi "IDE Contr
oller" 0:1
    ←[94mattachIso←[0m: Attach CD/DVD to the VM: attachIso win /os.iso "IDE Cont
roller" 0:1
    ←[94mattachUsb←[0m: Attach USB device to the VM (use listUsb to show availab
le devices): attachUsb win uuid
    ←[94mcloseportal←[0m: Close teleportation portal (see openportal,teleport):
closeportal Win

vboxshell.py, being a 100K byte code example is too hard to understand.
Why are those extra characters rendered ? Because Windows 'cmd'
console does not support colors ?
By and large I would like to see some *working*, yet
easy-to-understand examples.

On the side note: SDKref.pdf references VBox 3.3 once, which should be
changed to v4.0 (in chapter "3.4 VirtualBox events")

-Alexey Eromenko "Technologov"

More information about the vbox-dev mailing list