Skip to main content
Solved

Mysterious error in PythonCaller, undeclared variable "not defined"

  • November 8, 2018
  • 19 replies
  • 243 views

lifalin2016
Supporter
Forum|alt.badge.img+38

Hi.

I've just encountered a mysterious error in a PythonCaller in a workspace :

2018-11-07 20:17:02| 811.7| 3.9|WARN |Python Exception <NameError>: global name 'feature' is not defined

 

2018-11-07 20:17:02| 811.7| 0.0|WARN |Traceback (most recent call last):

 

File "<string>", line 16, in input

 

NameError: global name 'feature' is not defined

 

 

2018-11-07 20:17:02| 811.7| 0.0|ERROR |Error encountered while calling method `input'

 

2018-11-07 20:17:36| 843.3| 31.5|FATAL |Delete_temp_file(PythonFactory): PythonFactory failed to process feature

 

 

The problem is, that my code in the "Delete_temp_file" PythonCaller transformer doesn't have any variable named "feature" in it (I renamed it from the template's "feature" to "ft") :

import fme

 

import fmeobjects

 

import os

 

 

class FeatureProcessor(object):

 

def __init__(self):

 

pass

 

def input(self,ft):

 

try:

 

pass

 

pthnam = ft.getAttribute('tile_temppath')

 

os.remove(pthnam)

 

except:

 

pass

 

self.pyoutput(ft)

 

def close(self):

 

pass

 

 

Is the error resulting variable "feature" something that's hardcoded behind the scene ?

Cheers

Best answer by lifalin2016

Apparently the cause of the error is still as mysterious as when I wrote the question. As it's fairly easy to avoid (don't rename "feature" in input()), I'm closing the question.

This post is closed to further activity.
It may be an old question, an answered question, an implemented idea, or a notification-only post.
Please check post dates before relying on any information in a question or answer.
For follow-up or related questions, please post a new question or idea.
If there is a genuine update to be made, please contact us and request that the post is reopened.

19 replies

ebygomm
Influencer
Forum|alt.badge.img+45
  • Influencer
  • November 8, 2018

feature is not a variable in the template code, it should not be renamed


david_r
Celebrity
  • November 8, 2018

Also, you can safely delete all the "pass" lines inside the input()-method. The'yre not the source of the error, but the don't do anything either.


david_r
Celebrity
  • November 8, 2018

feature is not a variable in the template code, it should not be renamed

Actually, the code works for me both in FME 2018.1 and 2017.1 even when the "feature" parameter has been renamed in the input()-method.


david_r
Celebrity
  • November 8, 2018

Are you sure you've posted the complete code? I'm not able to reproduce the problem on my side.


ebygomm
Influencer
Forum|alt.badge.img+45
  • Influencer
  • November 8, 2018

Actually, the code works for me both in FME 2018.1 and 2017.1 even when the "feature" parameter has been renamed in the input()-method.

ah, so it does


david_r
Celebrity
  • November 8, 2018

Another thing, perhaps a bit unrelated: If you simply need a temporary file during your translation, I really recommend using the TempPathnameCreator rather than implementing something in Python.

Upsides of the transformer are:

  • Works seamlessly on Windows, Linux, OSX
  • Respects system settings that define temporary file locations (roaming profiles, etc)
  • No code to write and maintain

And best of all: FME will automatically deletes the temporary directories/files created using this transformer, so you don't have to. It even works if FME crashes.


lifalin2016
Supporter
Forum|alt.badge.img+38
  • Author
  • Supporter
  • November 8, 2018

Another thing, perhaps a bit unrelated: If you simply need a temporary file during your translation, I really recommend using the TempPathnameCreator rather than implementing something in Python.

Upsides of the transformer are:

  • Works seamlessly on Windows, Linux, OSX
  • Respects system settings that define temporary file locations (roaming profiles, etc)
  • No code to write and maintain

And best of all: FME will automatically deletes the temporary directories/files created using this transformer, so you don't have to. It even works if FME crashes.

The PythonCaller doesn't create a temp file, it deletes it.


david_r
Celebrity
  • November 8, 2018

The PythonCaller doesn't create a temp file, it deletes it.

Indeed, but I'm assuming that the file is created somewhere first :-)

But yeah, perhaps it doesn't apply to your case...


lifalin2016
Supporter
Forum|alt.badge.img+38
  • Author
  • Supporter
  • November 8, 2018

Also, you can safely delete all the "pass" lines inside the input()-method. The'yre not the source of the error, but the don't do anything either.

No, most of the "pass" lines are actually needed, as Python needs at least one line of code in any context.

 

I can see that the web UI has deleted all the relevant indention. How come the possibility to add un-reformatted code in this web UI has been removed ?

lifalin2016
Supporter
Forum|alt.badge.img+38
  • Author
  • Supporter
  • November 8, 2018

feature is not a variable in the template code, it should not be renamed

But why does FME then issue the error message then, naming the offending "feature" variable ?


david_r
Celebrity
  • November 8, 2018

No, most of the "pass" lines are actually needed, as Python needs at least one line of code in any context.

 

I can see that the web UI has deleted all the relevant indention. How come the possibility to add un-reformatted code in this web UI has been removed ?

There was an update to the forum update earlier this week and Safe is still trying to iron out some wrinkles.

Can you post the code as an attached .txt file? That should hopefully work.


david_r
Celebrity
  • November 8, 2018

But why does FME then issue the error message then, naming the offending "feature" variable ?

Difficult to tell since I cannot reproduce the issue. Is it possible to post a minimal sample workspace that reproduces the problem?


mark2atsafe
Safer
Forum|alt.badge.img+58
  • Safer
  • November 8, 2018

Can you try adding a feature into the close method? So the last few lines would be:

def close(self):
feature = fmeobjects.FMEFeature() 
pass 

From another case I've seen, I have a feeling this might help.


mark2atsafe
Safer
Forum|alt.badge.img+58
  • Safer
  • November 8, 2018

Can you try adding a feature into the close method? So the last few lines would be:

def close(self):
feature = fmeobjects.FMEFeature() 
pass 

From another case I've seen, I have a feeling this might help.

Sorry, that

 

character isn't required. We're having a few quirks with the latest knowledgebase update.


lifalin2016
Supporter
Forum|alt.badge.img+38
  • Author
  • Supporter
  • November 9, 2018

feature is not a variable in the template code, it should not be renamed

As a curiosity, and renamed "ft" back to "feature" in my code, and now it doesn't err !?!

 

 

Besides the error reference (line 16) was "def close" in which I only have a "pass" statement, prompting me to an assumption error in some hidden "code-behind".

lifalin2016
Supporter
Forum|alt.badge.img+38
  • Author
  • Supporter
  • November 9, 2018

Indeed, but I'm assuming that the file is created somewhere first :-)

But yeah, perhaps it doesn't apply to your case...

It's created by the output of a WMS call written by AttributeFileWriter.


lifalin2016
Supporter
Forum|alt.badge.img+38
  • Author
  • Supporter
  • November 9, 2018

There was an update to the forum update earlier this week and Safe is still trying to iron out some wrinkles.

Can you post the code as an attached .txt file? That should hopefully work.

FME_PythonCaller_error.txt


lifalin2016
Supporter
Forum|alt.badge.img+38
  • Author
  • Supporter
  • November 9, 2018

There was an update to the forum update earlier this week and Safe is still trying to iron out some wrinkles.

Can you post the code as an attached .txt file? That should hopefully work.

I renamed it back to its original, in which "feature" is replaced by "ft" in def input()


lifalin2016
Supporter
Forum|alt.badge.img+38
  • Author
  • Supporter
  • Best Answer
  • November 13, 2018

Apparently the cause of the error is still as mysterious as when I wrote the question. As it's fairly easy to avoid (don't rename "feature" in input()), I'm closing the question.