span8
span4
span8
span4
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 which 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 4 main places where Python can be used in FME:
*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 atFME Knowledge Center Q&A Forum.
Clickherefor 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.
Python can be downloaded from themain Python website.
For learning Python,there are many Python books available from publishers such asO'Reilly,etc.
Three free resources are:
FME installs its own Python interpreters which are used by any Python scripts running in FME on Windows unless otherwise specified.On Mac and Linux,FME uses the system Python interpreter.If you want FME to use a different Python interpreter,there is a setting in Workbench underTools > FME Options > Translation.
Please see this article for more information on how to use a custom interpreter:
Choosing a Different Python Interpreter (Installation)
Users can select which Python interpreter FME uses for the translation underTools > FME Options > Translation.Options include using FME's own Python interpreters,Esri ArcGIS interpreters or a custom interpreter.
FME is compatible with a number of Python versions.You can specify the level of compatibility with different Python versions with thePython Compatibilityworkspace parameter in the WorkbenchNavigatorpane.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 onPython Compatibility.
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 thePython 2.7 deprecation articlefor 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.
A number of FME related Python variables are available in FME Startup and Shutdown scripts.These variables are accessible in thefmemodule so you will need to include the statementimport fmeto access them.For example thefme.statusvariable can be true or false at the end of a translation depending 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 forStartup and Shutdown Python Scriptsin Workbench.
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 dictionaryfme.macroValues[].For example,if you want to get the value of the source dataset parameter for your AutoCAD reader and the parameter is calledSourceDataset_ACAD,you can access this parameter in Python like this:
import fmeSourceDataset = 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 theScripted Parameters article.
When a workspace is run,scripted parameters are executed in the order of appearance within the Navigator pane before startup scripts.
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,FME parameters,or use any of the FME methods such asgetAreaorreproject,etc.,with the API.To use FME Objects in your Python script,you will need to import FME Objects with the statement:
import fmeobjects
The FME Objects API documentation can be found here:FME Objects Python API
For guidance on when to use FME Objects,please see:When Should I Use FME Objects
For more information on the FME Objects Python API:Python FME Objects API
View the recording of theJune 2012 Python webinar.
Current: Python and FME Basics
Next: Startup Python Scripts in FME
Part 3: Python Scripted Parameters
Part 4: PythonCaller Transformer
Part 5: Shutdown Python Scripts in FME
© 2019 亚搏在线Safe Software Inc |Legal