In this post I’ll show how to set up a pyvmomi environment using modern Python tools such as pipenv. pyvmomi wasn’t difficult to setup before but it’s much easier now that the pyvmomi maintainers have taken a more modern approach to managing the pyvmomi distribution.
CentOS 7.6 was to perform these steps. Here are links to the resources that I used:
Getting the OS ready
We will need to install the following before installing the Python related tools:
sudo yum groupinstall -y “development tools”
sudo yum -y install git zlib-devel openssl-devel
Install pyenv
pyenv is a tool used to manage Python installations. It provides functionality such as letting you have multiple Python versions on the same machine. You can install it by running:
curl https://pyenv.run | bash
At the end of the installation it instructs you to add the following to your ~/.bashrc file:
add to .bashrc
export PATH=”/home/chris/.pyenv/bin:$PATH”
eval “$(pyenv init -)”
eval “$(pyenv virtualenv-init -)”
Re-log into your shell after this is done.
Install Python 3.6
pyvmomi says the latest version that it’s compatible with is 3.6 so let’s install that. You can list all available Python distributions by running:
pyenv install –list
Install Python 3.6.0
pyenv install -v 3.6.0
Verify that 3.6.0 was installed
pyenv versions
system
* 3.6.0 (set by /home/chris/.python-version)
Install pipenv
pipenv is a tool used to manage Python projects and uses virtual environments in the background. We can install pipenv with pip. If you don’t have pip, you can install it by running:
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
python get-pip.py –user
Install pipenv:
pip install pipenv –user
Install a Python 3 (3.6.0) virtual environment:
pipenv install –three
Start up a new shell using the virtual environment:
pipenv shell
Verify the Python version:
python -V
Python 3.6.0
Install pyvmomi community samples
Clone the pyvmomi Github repo:
git clone https://github.com/vmware/pyvmomi-community-samples.git
Change into the samples directory:
cd pyvmomi-community-samples/
Install everything:
python setup.py install
Run a sample script that will return all VMs in a vCenter:
./samples/getallvms.py -S -s vc65b.vmware.local -u cloudadmin -p VMware1!
There are 78 scripts in the samples directory that you can experiment with and adapt as needed.