Skip to main content
Solved

Mysterious error in PythonCaller, undeclared variable "not defined"


lifalin2016
Contributor
Forum|alt.badge.img+29

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.

View original
Did this help you find an answer to your question?

19 replies

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

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


david_r
Evangelist
  • 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
Evangelist
  • November 8, 2018
ebygomm wrote:

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
Evangelist
  • 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+32
  • Influencer
  • November 8, 2018
david_r wrote:

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
Evangelist
  • 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
Contributor
Forum|alt.badge.img+29
  • Author
  • Contributor
  • November 8, 2018
david_r wrote:

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
Evangelist
  • November 8, 2018
lifalin2016 wrote:

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
Contributor
Forum|alt.badge.img+29
  • Author
  • Contributor
  • November 8, 2018
david_r wrote:

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
Contributor
Forum|alt.badge.img+29
  • Author
  • Contributor
  • November 8, 2018
ebygomm wrote:

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
Evangelist
  • November 8, 2018
lifalin2016 wrote:

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
Evangelist
  • November 8, 2018
lifalin2016 wrote:

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+44
  • 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+44
  • Safer
  • November 8, 2018
mark2atsafe wrote:

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
Contributor
Forum|alt.badge.img+29
  • Author
  • Contributor
  • November 9, 2018
ebygomm wrote:

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
Contributor
Forum|alt.badge.img+29
  • Author
  • Contributor
  • November 9, 2018
david_r wrote:

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
Contributor
Forum|alt.badge.img+29
  • Author
  • Contributor
  • November 9, 2018
david_r wrote:

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
Contributor
Forum|alt.badge.img+29
  • Author
  • Contributor
  • November 9, 2018
david_r wrote:

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
Contributor
Forum|alt.badge.img+29
  • Author
  • Contributor
  • 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.


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings