Getting Started with pSphere on OSX

by bunchc on July 9, 2012

Always on the search for new skills and new ways to work with vSphere & Virtualization, I’ve been turned on to pSphere (Python bindings for vSphere). However, getting this up and going on my Mac was not as straight forward as I’d have thought.

Note: I’m not sure what is strictly required here, these are the steps that worked for me.


You’ll need to install the following:

Installing PIP:

sudo easy_install pip

Installing virtualenv

sudo pip install virtualenv

Installing libyaml

brew install libyaml

Installing pSphere

Now that you have the prerequisites going, lets install pSphere:

Create the Virtual Environment:

$ virtualenv psphere_test
New python executable in psphere_test/bin/python
Installing setuptools…………done.
Installing pip……………done.

Change into the Virtual Environment

$ pwd
M102159AGY:binprovmware$ ls
activate        easy_install        python
activate.csh        easy_install-2.7    python2.7        pip    pip-2.7
M102159AGY:bin provmware$ source activate
(psphere_test)M102159AGY:bin provmware$

Installing pSphere

$ pip install psphere

Connecting to vCenter

>>> from psphere.client import Client
>>> client = Client(vCenter_IP, vCenter_User, vCenter_Pass)

Listing VMs

First you need to import the object class for Virtual Machines:

>> from psphere.managedobjects import VirtualMachine

Now, you can collect a list of all VMs and check out the contents:

>>> vms = VirtualMachine.all(client)
>>> vms
[<psphere.managedobjects.VirtualMachine object at 0x111419f50>, <psphere.managedobjects.VirtualMachine object at 0x111419f10>, <psphere.managedobjects.VirtualMachine object at 0x111419fd0>, <psphere.managedobjects.VirtualMachine object at 0x1114353d0>, <psphere.managedobjects.VirtualMachine object at 0x1114352d0>, <psphere.managedobjects.VirtualMachine object at 0x111435290>, <psphere.managedobjects.VirtualMachine object at 0x111435350>, <psphere.managedobjects.VirtualMachine object at 0x111435310>, <psphere.managedobjects.VirtualMachine object at 0x111435390>, <psphere.managedobjects.VirtualMachine object at 0x111435410>, <psphere.managedobjects.VirtualMachine object at 0x111435450>, <psphere.managedobjects.VirtualMachine object at 0x111435490>, <psphere.managedobjects.VirtualMachine object at 0x1114354d0>, <psphere.managedobjects.VirtualMachine object at 0x111435550>, <psphere.managedobjects.VirtualMachine object at 0x111435510>, <psphere.managedobjects.VirtualMachine object at 0x1114355d0>, <psphere.managedobjects.VirtualMachine object at 0x111435610>, <psphere.managedobjects.VirtualMachine object at 0x111435650>]

Kinda boring, but it is indeed a list of all VMs. Now, let’s find out a bit more about them:

This first one provides the name of the VM.

>>> vms[0].name

Now things get a bit more… verbose:
>>> vms[0].summary
   vm = <psphere.managedobjects.VirtualMachine object at 0x111419c50>
   runtime =
         device[] =
               runtimeState =
                     vmDirectPathGen2Active = False
                     vmDirectPathGen2InactiveReasonOther[] =
               key = 4000
               runtimeState =


Like PowerCLI, vCO, and some of the other VMware provided SDKs, pSphere provides a 3rd party way of accessing the vSphere API via Python.

  • Dan Jellz

    Works great in Fedora 17 as well. Only difference was to YUM install libyaml.  Thanks for the great tutorial!

  • Patrick A

    So where’s the script and output to json ? You should start a tool on github, and release some codez

  • professionalvmware

    Interesting idea, and would you have some ideas? I had a few, then well, got writers block.

Previous post:

Next post: