Using Salt with Python 3 and Pyvenv

Posted on Sun 02 October 2016 in python

It's simple to make Salt use Python 3's pyvenv to create a Python virtual environment.

Example: Using Pyvenv to Install Errbot

This example assumes that Python 3.5 is already available on the system as a separate install.

And in case you've never heard of it before, Errbot is simply a python-based chatbot.

venv-directory:
  file.directory:
    - name: /opt/venv
    - makedirs: True

errbot-virtualenv:
  virtualenv.managed:
    - name: /opt/venv/errbot
    - requirements: salt://files/requirements.txt
    - cwd: /opt/venv/errbot/bin
    - venv_bin: /usr/bin/pyvenv-3.5
    - require:
      - file: venv-directory

The first block ("venv-directory") simply makes a parent directory in /opt called venv to store this (and future) virtual envs.

The second block ("errbot-virtualenv") is the meat:

  • name: path where the virtual env will exist
  • requirements: location of requirements.txt, if you have one
  • cwd: path to where the pip executable is inside the virtual env
  • venv_bin: where the pyvenv executable can be specified
  • require: prevents the virtual env from being created until venv-directory completes

Even though this took me probably longer than it should to figure out, I hope you can take advantage of this and avoid the time sink.