FME Version
Introduction
This article is part 1 of a 5-part series to help you get started with basic Python and FME. The series will include an introduction to the Python programming language and also cover the main applications of Python in FME: startup Python scripts, scripted parameters, PythonCaller/PythonCreator transformers, and shutdown Python scripts. This article introduces you to the Python programming language and includes basic information about using Python in FME Workbench.
Python is a programming language that can be used within FME to accomplish tasks either before or after FME runs or to perform tasks within FME that are not possible with standard FME tools and transformers*. For example, you can use a Python script to move data before or after a translation. There are four main places where Python can be used in FME:
- Startup Python Script
- Scripted Parameter
- PythonCaller and PythonCreator Transformers
- Shutdown Python Script
*Important Note: There are numerous FME transformers for performing almost every task you can think of without resorting to scripting. If you are thinking about writing a Python script to do something in FME you should make absolutely sure there is not an existing transformer available already. Please feel free to ask the FME community.
Click here for an interesting blog post written by our FME Evangelist about his experience with using a combination of tools available in FME, including Python scripting and custom transformers, to enhance his FME project.
Basic Resources on Programming in Python
For learning Python, there are many Python books available from publishers such as O'Reilly, etc.
Three free resources are:
Python Interpreter
FME installs its own Python interpreter, which is used by any Python scripts running in FME on Windows and macOS. On Linux, FME uses the system Python interpreter. If you want FME to use a different Python interpreter (Windows only), there is a setting in Workbench under Tools > FME Options > Translation.
Please see this article for more information on how to use a custom interpreter: Choosing a Different Python Interpreter (Installation)
This per-user setting controls:
- Default Python Compatibility value for new workspaces
- Interpreter loaded for FME Python’s interactive shell
- The version of modules installed using FME Python’s pip utility
Note: this parameter does not affect Python interpreter choice for existing workspaces. The Python Compatibility parameter should be used instead.
Users can select the default Python Compatibility value for new workspaces under Tools > FME Options > Translation. Options include using FME's own Python interpreters, Esri ArcGIS interpreters (Windows). A custom interpreter can also be specified with this setting.
Python Compatibility
FME is compatible with a number of Python versions. You can specify a workspace or custom transformer's level of compatibility with different Python versions with the Python Compatibility workspace parameter in the Workbench Navigator pane. For example, Python 3.0 introduced significant changes that are not backwards compatible with Python 2.x so it is important to set the most optimal compatibility level for your workspace. Please see this page for more information on Python Compatibility.
This parameter is workspace specific. It determines the Python interpreter used by FME when running a workspace.
Note that starting in FME 2019, Python 2.7 will be an optional installation component for the Windows platform and will not be installed using default installation settings. Please see the Python 2.7 deprecation article for more details.
Users can specify the Python Compatibility for the workspace within the Navigator pane under Workspace Parameters > Scripting > Python Compatibility.
Note: The Python Compatibility parameter in the workspace Navigator window takes precedence over the Python Interpreter setting in FME Options.
FME Variables in Python Scripts
A number of FME-related Python variables are available in FME Startup and Shutdown scripts. These variables are accessible in the fme module, so you will need to include the statement import fme to access them. For example, the fme.status variable can be true or false at the end of a translation depends on the success or failure of the workspace. A complete list of FME variables available for Startup and Shutdown Python scripts, respectively, can be found on the Help page for Startup and Shutdown Python Scripts in Workbench.
FME Parameters in Python Scripts
One of the FME-related variables, fme.macroValues[], is a dictionary of each FME parameter known when the workspace is run. The dictionary is indexed by parameter names and holds the parameter values.
These parameter values are often used in a Python script, for example, when you want to know the file path of the source dataset used in a reader or some other user-defined parameter. You can get the values of FME parameters by accessing the dictionary fme.macroValues[]. For example, if you want to get the value of the source dataset parameter for your AutoCAD reader and the parameter is called SourceDataset_ACAD, you can access this parameter in Python like this:
import fme SourceDataset = fme.macroValues['SourceDataset_ACAD']
We can also set FME parameters by creating a scripted parameter and returning the value from the script. This is covered in the Scripted Parameters article.
When a workspace is run, scripted parameters are executed in the order of appearance within the Navigator pane before startup scripts.
FME Objects and Python
Much of FME's core functionality is available within Python scripts through the use of FME classes and methods from the FME Objects Python API. For example, you can access the FME log file and FME parameters or use any of the FME methods, such as getArea or reproject, etc., with the API. To use FME Objects in your Python script, you will need to import FME Objects with the statement:
import fmeobjects
AI Assist for Python
For more information on extending the FME Objects API by using the FME SDK, please see Developing Third-Party Integrations with FME.
Articles
Shutdown Python Scripts in FME
Comments
0 comments
Please sign in to leave a comment.