Run an FME Workspace from Python using FMEWorkspaceRunner

Liz Sanderson
Liz Sanderson
  • Updated

FME Version

  • FME 2020.0

Introduction

To run an FME workspace using python outside of FME Workbench you can edit the attached sample and include it within your python script. For example, an ArcGIS geoprocessing script written in Python could launch an FME Workspace to preprocess some data or translate some data output.


Requirements

  • You must have FME Desktop installed on the machine running the Python script.
  • You need to know the path to your FME workspace and the required published parameters of your workspace. For information on creating and modifying published parameters within your workspace, see the documentation.
  • If you are using a Python installation which is not the one provided by FME then you must ensure the Python version is compatible by the version of FME used. In addition, you must include the path to fmeobjects module (<FMEInstallDirectory>\fmeobjects\python<major><minor>) within your Python search path. You can add this path by including it in an environment variable called PYTHONPATH or by adding the path to fmeobjects directly within your Python script. For example:
import sys
sys.path.append(r"C:\Program Files\FME\fmeobjects\python38")

 

Script

The script below will run your workspace with the specified parameters and return either a success message or an FME Exception. Download the attached version of the script to better preserve indentations etc. Edit the path to the workspace where you see the workspace variable and the published parameters names and values where you see the parameters dictionary being created to reflect your own environment.

The script uses the FMEWorkspaceRunner class and the runWithParameters() method found within fmeobjects. You can also use the promptRun() method to prompt for published parameters by uncommenting the appropriate line below.

This script contains Python 3 specific syntax and may fail when run in Python 2.

import sys
sys.path.append(r"C:\Program Files\FME\fmeobjects\python38")

import fmeobjects

# initiate FMEWorkspaceRunner Class
runner = fmeobjects.FMEWorkspaceRunner()

# Full path to Workspace, example comes from the FME 2020 Training Dataset
workspace = r"C:\FMEData2020\Workspaces\IntroToDesktop\Ex2.1-Complete.fmw"

# Set workspace parameters by creating a dictionary of name value pairs
parameters = {}
parameters["SourceDataset_XLSXR"] ="https://s3.amazonaws.com/FMEData/Interopolis/CaseLocationsDetails_2017_XLS.zip"
parameters['DestDataset_CSV2'] = r"C:\FMEData2020\Output\Training"

# Use try-except block so we can get FME Exception
try:
    # Run Workspace with parameters set in above dictionary
    runner.runWithParameters(workspace, parameters)
    
    # Or use promptRun to prompt for published parameters
    # runner.promptRun(workspace)
    
    # Tell user the workspace ran
    print(f"The Workspace: {workspace}")
    print("...ran successfully")
    
except fmeobjects.FMEException as ex:
    # Print out FME Exception if worskspace failed
    print(ex.message)

# get rid of FMEWorkspace runner so we don't leave an FME process running
runner = None


Installed Example

An "official" example of running a workspace with Python can be found in the FME installation folder (eg. C:\Program Files\FME\fmeobjects\samples\Python\WorkspaceRunner.py) if the Install SDK option was selected when installing FME.
 

Find all of the workspace running options for the Python interface in the FME documentation at: https://docs.safe.com/fme/html/fmepython/api/fmeobjects/_utilities/fmeobjects.FMEWorkspaceRunner.html

Was this article helpful?

Comments

0 comments

Please sign in to leave a comment.