Python

Overview

Python scripts can be automated in Zuar Runner via command line jobs.

Command Line Job

Upload a Python Script to Zuar Runner

Any Python scripts to be run by Zuar Runner must exist on Zuar Runner’s file system. Use Zuar Runner’s Files to upload the

Python script.

Create a Python Virtual Environment

This step only needs to be performed once.

If your Python scripts require any packages outside of Python’s standard library, you should create a Python virtual environment and install your packages into that environment.

We use Python’s venv module for creating separate Python virtual environments.

Create a command line job that runs this command:

/app/env/bin/python3 -m venv /var/mitto/data/{virtualenv_directory}

Replace {virtualenv_directory} with a directory name for your virtual environment.

Run this job once to create a directory and initialize a new Python virtual environment in the directory. This directory will be referenced when you want to use this Python virtual environment.

Install Python Packages in the Python Virtual Environment with Pip

This step only needs to be performed once.

Create a command line job that runs this command:

/var/mitto/data/{virtualenv_directory}/bin/pip3 install {python_package}

Replace {virtualenv_directory} with the directory name of your virtual environment and replace {python_package} with the name of your Python package.

Run this job once to install a Python package in your Python virtual environment using pip.

Note you can install multiple Python packages at once like this:

/var/mitto/data/{virtualenv_directory}/bin/pip3 install {python_package1} {python_package2} {python_package3}

Create a Command Line Job to Run a Python Script

Create a command line job that runs this command:

/var/mitto/data/{virtualenv_directory}/bin/python3 /var/mitto/data/{python_script}.py

Every time this job is run, it will execute the Python script using the Python virtual environment.

Example Python Script

The following script (test_python_script.py) will create a CSV: written_by_python.csv.

#!/usr/bin/env python3
"""NOTE: You must use python3"""
with open("/var/mitto/data/written_by_python.csv", "w") as f:
    f.write("id,name\n")
    f.write("1,Allen\n")
    f.write("2,Steve\n")

One thing to note is that python won’t work, as Zuar Runner has python3 installed.

Running the job should create a file that looks like this:

../_images/python__image-1.png