Python Access to vSphere APIsLast Updated December 16, 2024
For
the Python developer, this chapter shows how to install the Python SDKs and run example
programs against vSphere APIs.
VMware vSphere offers two important
programming interfaces: (1) Web Services API in the vSphere Management SDK, which is
SOAP based and the integration point for
pyvmomi
. (2) vSphere
Automation API, which is primarily REST based but has additional bindings for Python and
Java. SOAP interfaces were first used decades ago to implement the vSphere Client UI.
The Automation API is newer with additional features and some duplication. Code samples
in this chapter use them together.Prerequisite:
Python 3.8 or higher. Recent Linux distributions and MacOS include higher Python
versions. The command name is probably
python3
but can be
aliased.Installing the Python SDKs for
vSphere
Python access to vSphere APIs is most
convenient with the
pyvmomi
library. To install
pyvmomi
:- If not already installed, get the Python installerpip. On Ubuntu systems, the package is namedpython-pip. On MacOS there are several alternatives; one is to download and runget-pip.py. In any case, addpip's location to your PATH.
- Updatepipand setup tools, then install (or upgrade)pyvmomiwith thepipcommand.
- Install the vSphere Automation SDK for Python from its Github archive. The expressiongit+httpsmeans to use secure HTTP for downloading the Github install.You could also download the vSphere Automation SDK for Python (as a ZIP file) from its https://developer.broadcom.com/sdks landing page. Install instructions, including for a secure air-gapped environment, are available in the Quick Start Guide section of README.md on the http://github.com/vmware/vsphere-automation-sdk-python web page.
- Now you are ready to run the code samples below. Copy and paste the code into a file and run thepythonorpython3command on the file. You can run one at a time, or all three copy-and-pastes together at once.
Basic Authentication Using
pyvmomi
The
pyvmomi
SDK simplifies code for connecting to the VIM endpoint
by using a SmartConnect
function that accepts basic user
credentials, opens a session with the server, and returns the
ServiceInstance
object. ServiceInstance
contains references to specific managed objects needed to use VIM API features.The
vsphere_client
object contains interface stubs for all
Automation API endpoints. This code excerpt shows a function that connects to both
the VIM endpoint and the Automation API endpoint and returns both the
vsphere_client
object and the ServiceInstance
object.At this point, your session is established and the session ID is stored in a cookie
that will be passed along with future requests.
Creating a Hello Folder with the
VIM API Using pyvmomi
The
ServiceContent
object contains a reference to the root folder of the managed object hierarchy. You
can invoke the CreateFolder()
method on the
rootFolder
object to create a subfolder named Hello
Folder
.Tagging the Folder with
Automation API for Python
To tag a folder, start by creating a tag
category, then create a tag in that category, and finally attach the tag to the
folder.
The following image shows the Hello
Folder and Category as it looks in the vSphere Client UI.

What To Do Next
You can download community-sourced
pyvmomi
examples three different ways:- gitclonehttps://github.com/vmware/pyvmomi-community-samples