Skip to main content
Solved

Within one PythonCaller script, are you able to call functions defined in other PythonCaller scripts in the FME workbench ?


Forum|alt.badge.img+1

Would it be possible to create a script in one PythonCaller with a number of functions, and then call the functions in that script from another PythonCaller using the 'from <script_name> import <function_name>' syntax?

Best answer by david_r

Yes, you can, but not exactly as you propose. For example, you can import modules that are saved in the same directory as your workspace file. This works because FME automatically adds the current workspace directory to the PythonPath when the workspace is running. This is also mentioned in the log.

Since the entire workspace is using the same Python interpreter, it is also possible to reference objects between different PythonCallers, but there are some potential pitfalls so this is something that I always try to avoid.

But as an example, you can define a global variable in the startup script and retrieve the value in e.g. a PythonCaller later on.

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

4 replies

david_r
Evangelist
  • Best Answer
  • February 4, 2021

Yes, you can, but not exactly as you propose. For example, you can import modules that are saved in the same directory as your workspace file. This works because FME automatically adds the current workspace directory to the PythonPath when the workspace is running. This is also mentioned in the log.

Since the entire workspace is using the same Python interpreter, it is also possible to reference objects between different PythonCallers, but there are some potential pitfalls so this is something that I always try to avoid.

But as an example, you can define a global variable in the startup script and retrieve the value in e.g. a PythonCaller later on.


Forum|alt.badge.img+1
  • Author
  • February 4, 2021

Thanks @david_r​ for your quick reply. What would be some of the pitfalls of referencing objects between different PythonCallers? Also, when you talk about defining a global variable in the startup script and retrieving it in the PythonCaller, how would I do that when the variable is another PythonCaller?


david_r
Evangelist
  • February 4, 2021
aquamarine wrote:

Thanks @david_r​ for your quick reply. What would be some of the pitfalls of referencing objects between different PythonCallers? Also, when you talk about defining a global variable in the startup script and retrieving it in the PythonCaller, how would I do that when the variable is another PythonCaller?

Pitfalls would be scope and order of execution, amongst other thing. But this is not something I have tested extensively.

I believe you can define a global variable in one PythonCaller and reference it in another, but it's not a very elegant or transparent solution, I find.


Forum|alt.badge.img+1
  • Author
  • February 4, 2021

ok great thank you for explaining!


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