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

Discussion about using the VirtualBox API, Tutorials, Samples.

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

Postby GenghisKhan » 22. Jan 2019, 16:22

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   Expand viewCollapse view
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   Expand viewCollapse view
import datetime
import vboxapi

def virtual_machines_master():
    inicial_date = datetime.datetime.now()
    virtualboxmanager = vboxapi.VirtualBoxManager(None, None)
    vbox = virtualboxmanager.getVirtualBox()
    session = virtualboxmanager.getSessionObject(vbox)
    session.console.takeSnapshot('Snapshot '+inicial_date.strftime("%y%m%d"), 'Snapshot taken through vboxapi')

if __name__=="__main__":

System specs:
- Python 3.6.5
- VirtuaBox 5.2.16
- Windows 10
Posts: 9
Joined: 23. Feb 2018, 17:54

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

Postby klaus » 23. Jan 2019, 18:26

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.
Oracle Corporation
Posts: 783
Joined: 10. May 2007, 14:57

Return to The VirtualBox API

Who is online

Users browsing this forum: No registered users and 0 guests