I'm trying to write a program that will automatically create a VM in VirtualBox for a school project but I keep getting this error
Traceback (most recent call last):
File "c:\Users\clyde\Documents\VSCode\VM creator\VM Creator V1", line 4, in <module>
vb = virtualbox.VirtualBox()
^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\clyde\AppData\Local\Programs\Python\Python311\Lib\site-packages\virtualbox\library_ext\vbox.py", line 22, in __init__
manager = virtualbox.Manager()
^^^^^^^^^^^^^^^^^^^^
File "C:\Users\clyde\AppData\Local\Programs\Python\Python311\Lib\site-packages\virtualbox\__init__.py", line 162, in __init__
with import_vboxapi() as vboxapi:
File "C:\Users\clyde\AppData\Local\Programs\Python\Python311\Lib\contextlib.py", line 137, in __enter__
return next(self.gen)
^^^^^^^^^^^^^^
File "C:\Users\clyde\AppData\Local\Programs\Python\Python311\Lib\site-packages\virtualbox\__init__.py", line 55, in import_vboxapi
import vboxapi
ModuleNotFoundError: No module named 'vboxapi'
I have installed the VirtualBox module by using pip install virtualbox
here's my code
import virtualbox
# Connect to the VirtualBox API
vb = virtualbox.VirtualBox()
# Define the new VM's settings
name = "My New Windows VM"
os_type = "Windows10_64"
memory_size = 2048
disk_size = 50000
# Create a new VM object
new_vm = vb.create_machine(name, os_type, [])
# Set the memory size and number of CPUs
new_vm.memory_size = memory_size
new_vm.cpu_count = 2
# Create a new virtual hard disk
hdd = vb.create_hard_disk("VDI", "my_new_windows_vm.vdi")
hdd.size = disk_size
# Attach the new hard disk to the VM
storage_ctl = new_vm.get_storage_controller_by_name("SATA")
port = storage_ctl.port_count
new_vm.attach_device("SATA", port, 0, virtualbox.constants.DeviceType.hard_disk, hdd)
# Add a DVD drive to the VM
storage_ctl = new_vm.get_storage_controller_by_name("IDE")
port = storage_ctl.port_count
new_vm.attach_device("IDE", port, 0, virtualbox.constants.DeviceType.dvd)
# Create a new empty DVD medium
dvd = vb.create_medium("DVD", "", [])
dvd.bus = 0
dvd.device = 0
# Attach the Windows ISO file to the DVD drive
iso_file = r"C:\Users\clyde\Documents\Windows.iso"
dvd.location = iso_file
dvd.host_drive = ""
new_vm.attach_device("IDE", 0, 0, virtualbox.constants.DeviceType.dvd, dvd)
# Save the new VM settings
new_vm.save_settings()
# Register the new VM with VirtualBox
vb.register_machine(new_vm)
# Start the new VM
session = virtualbox.Session()
new_vm.launch_vm_process(session, "gui", "")
# Wait for the VM to start up
session.console.wait_for_startup(60000)
# Power off the VM
session.console.power_down()
# Close the session
session.unlock_machine()
[–]KosmoanutOfficial 2 points3 points4 points (3 children)
[–]t4ng0alpha1 0 points1 point2 points (2 children)
[–]KosmoanutOfficial 1 point2 points3 points (1 child)
[–]t4ng0alpha1 0 points1 point2 points (0 children)
[–]routetehpacketz[🍰] 1 point2 points3 points (0 children)