[Python] NameError: name 'LockType' is not defined

Discussion about using the VirtualBox API, Tutorials, Samples.
Post Reply
GenghisKhan
Posts: 9
Joined: 23. Feb 2018, 17:54

[Python] NameError: name 'LockType' is not defined

Post by GenghisKhan »

Hello everyone,

I am trying to create snapshots in python through vboxapi, but since I never used vboxapi before, I feel a bit lost when I dive in documentation and examples from python, java and C# to know the right steps to accomplish this.

So I came to this script where I am getting,

Code: Select all

NameError: name 'LockType' is not defined


Where I am calling LockType in a possible wrong way or due to missing library importation, but I really dont know how to set it.

Here is my actual script:

Code: Select all

import datetime
import vboxapi

def virtual_machines_master():
    inicial_date = datetime.datetime.now()
    virtualboxmanager = vboxapi.VirtualBoxManager(None, None)
    vbox = virtualboxmanager.getVirtualBox()
    usb_server_1=vbox.findMachine("usb-server-1")
    session = virtualboxmanager.getSessionObject(vbox)
    usb_server_1.lockMachine(session,LockType.LockType_Shared)
    session.console.takeSnapshot('Snapshot '+inicial_date.strftime("%y%m%d"), 'Snapshot taken through vboxapi')
    session.unlockMachine()

if __name__=="__main__":
    virtual_machines_master()
System specs:
- Python 3.6.5
- VirtuaBox 5.2.16
- Windows 10
klaus
Oracle Corporation
Posts: 1110
Joined: 10. May 2007, 14:57

Re: [Python] NameError: name 'LockType' is not defined

Post by klaus »

There are some examples in the SDK manual, and vboxshell.py also uses several enum consts. None of these look like what you do. Seems you copied something from a Java example or some such.

For Python the access to enum constants works with vboxmgr.constants.LockType_Shared and the like. vboxmgr is the object returned by the VirtualBoxManager(...) constructor.

Oh, and there's also vboxmgr.openMachineSession(...) which creates a session and by default gets a lock of type LockType_Shared, plus a matching closeMachineSession(). This is used by vboxshell.py.
Post Reply