Skip to main content
Hi,

 

 

I am using FME 2012 and would like to write a startup python script in which I can import a python module and call that module's function. I used to be able to do this with pyfme but with FME 2012 I have to use fmeobjects and it throws an error when I do. Two simple test cases below.

 

 

What might be causing the error? Looks like when I import the custom module, fmeobjects is not getting recognized.

 

 

Embedded script (works):

 

import fmeobjects

 

 

def audit_start():

 

    logger = fmeobjects.FMELogFile()

 

    logger.logMessageString("audit_start()")

 

    for name,val in FME_MacroValues.iteritems():

 

        logger.logMessageString( "   %s:%s" %(name,val))

audit_start()

 

 

Imported script (error):

 

import audit

 

audit.audit_start()

audit.py

 

import fmeobjects

 

 

def audit_start():

 

    logger = fmeobjects.FMELogFile()

 

    logger.logMessageString("audit_start()")

 

    for name,val in FME_MacroValues.iteritems():

 

        logger.log( "   %s:%s" %(name,val))

 

 

Error: Python Exception <NameError>: global name 'FME_MacroValues' is not defined
Solved. __main__ namespace have to be imported:

 

 

import __main__

 

import fmeobjects

 

 

def audit_start():

 

    logger = fmeobjects.FMELogFile()

 

    logger.logMessageString("audit_start()")

 

    for name,val in __main__.FME_MacroValues.iteritems():

 

        logger.log( "   %s:%s" %(name,val))

 

 

 


Just to mention, pyfme was replaced in FME2012 to solve a bunch of problems and improve our Python support. You can find more details at:

 

 

http://fmepedia.safe.com/articles/How_To/New-Python-FME-Objects-API-in-FME-2012
Another suggestion would be to add the log file object and the FME_MacroValues dictionary as a parameter to audit_start(), e.g.:

 

 

In the workspace

 

import audit

 

audit.audit_start(fmeobjects.FMELogFile(), FME_MacroValues)

 

 

In audit.py

 

def audit_start(myFMELogger, myMacroValues):

 

    myFMELogger.logMessageString("audit_start()")

 

    for name,val in myMacroValues.iteritems():

 

        myFMELogger.log( "   %s:%s" %(name,val))

 

 

I would consider this a slightly better approach as the coupling between FME (the caller) and your external script is less tight.

 

 

David

Reply