Kostas Demiris
Kostas Demiris

Reputation: 3631

Starting a VirtualBox VM from a Python Script

I have this simple script..that does not work

import subprocess
subprocess.call(["C:\Program Files\Oracle\VirtualBox\VBoxManage.exe", "VBoxManage startvm WIN7"])

I have the same thing in a bat file...which works perfectly.

cd C:\Program Files\Oracle\VirtualBox
VBoxManage startvm "WIN7"

I have the VBoxManage.exe in the PATH of Windows 8.1 (My host OS).

The python script understands the VBoxManage executable and spits out it's manual and then this ..

Syntax error: Invalid command 'VBoxManage startvm WIN7'

Could you give me a way to start a VM from inside a python script, either by invoking the .exe directly or by running the .bat file ?

Note: I have searched for the vboxshell.py file but not found it anywhere...:[

Upvotes: 2

Views: 3711

Answers (2)

Kostas Demiris
Kostas Demiris

Reputation: 3631

The trick is to pass the command as separate arguments

import subprocess 
subprocess.call(["C:\Program Files\Oracle\VirtualBox\VBoxManage.exe", "startvm", "WIN7"]) 

Upvotes: 1

Andrew
Andrew

Reputation: 5352

subprocess.call() expects a list of arguments, like so:

subprocess.call(['C:\Program Files\Oracle\VirtualBox\VBoxManage.exe',
                 'startvm',
                 'WIN7'])

Your code passes 'VBoxManage startvm WIN7' as a single argument to VBoxManage.exe, which expects to find only a command (e.g. 'startvm') there. The subsequent arguments ('WIN7' in this case) need to be passed separately.

In addition, there is no need to repeat the executable name when using subprocess.call(). The example from the Python docs invokes the UNIX command "ls -l" as follows:

subprocess.call(['ls', '-l'])

In other words, you don't need to repeat the 'VBoxManage' part.

Upvotes: 1

Related Questions