execute_script | Script Method | Teradata Python Package - 17.00 - execute_script - Teradata Package for Python

Teradata® Package for Python User Guide

Teradata Package for Python
Release Number
Release Date
November 2021
Content Type
User Guide
Publication ID
English (United States)

Use the execute_script to run script on Vantage.

This method does not have any arguments.


This example shows a workflow to create a Script object, install Python script on Vantage, runs it and then removes the script from Vantage.

The script "mapper.py" reads in a line of text input ("Old Macdonald Had A Farm") from csv and splits the line into individual words, emitting a new row for each word.

To run this example, "mapper.py" and "barrier.csv" are required and must be present under the same location specified by the argument files_local_path.
  • "barrier.csv" is present under <teradataml_install_location>/teradataml/data directory.
  • "mapper.py" can be created as follows:
    import sys
    for line in sys.stdin:
        line = line.strip()
        words = line.split()
        for word in words:
            print ('%s\t%s' % (word, 1))
  • Load example data.
    >>> load_example_data("Script", ["barrier"])
  • Import required packages.
    >>> from collections import OrderedDict
    >>> from teradatasqlalchemy import (VARCHAR)
  • Create teradataml DataFrame.
    >>> barrierdf = DataFrame.from_table("barrier")
  • Create a Script object that allows user to run script on Vantage.
    >>> sto = Script(data=barrierdf,
                files_local_path= 'data/scripts',
                script_command='python3 ./<database name>/mapper.py',
                charset='latin', returns=OrderedDict([("word", VARCHAR(15)), ("count_input", VARCHAR(2))])
  • Install script on Vantage.
    >>> sto.install_file(file_identifier='mapper', file_name='mapper.py', is_binary=False)
  • Set the search path to the database where the file is installed.
    >>> get_context().execute("SET SESSION SEARCHUIFDBPATH = <database name>;")
  • Run the user script on Vantage.
    >>> sto.execute_script()
    ############ STDOUT Output ############
            word count_input
    0  Macdonald           1
    1          A           1
    2       Farm           1
    3        Had           1
    4        Old           1
    5          1           1
  • Remove the installed file from Vantage.
    >>> sto.remove_file(file_identifier='mapper', force_remove=True)