Skip to main content
Solved

In FME Desktop, is it possible to run a python script that is stored on disk, and not within the Python caller?

  • March 1, 2023
  • 8 replies
  • 76 views

I have a Python script that executes a set of tools in QGIS. I would like to execute this Python script through FME.

In FME Desktop, is it possible to run a python script that is stored on disk, and not within the Python caller?

Best answer by lifalin2016

Hi.

Not really possible with the built-in PythonCaller (defined in fmesuite.py):

PARAMETER_NAME: PYTHONSOURCE
# note: not this is not or_attr because we do not want
# expression wrapping.  
PARAMETER_TYPE: OPTIONAL TEXT_EDIT FME_SYNTAX%PYTHON%FME_INLINE%YES
PARAMETER_PROMPT: Python Script:
PARAMETER_DEFAULT: import<space>fme<lf>import<space>fmeobjects<lf>...

You could build another version yourself with such a feature, but it sounds more like you want to have an external tool library (module) available for your Python code, and this is very possible.

Such modules need to be stored under either %FME_HOME%/Python or %APPDATA%/Roaming/Python, or at least in a folder included in the environment variable PYTHON_PATH.

Then just import it into your Python code.

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.

8 replies

lifalin2016
Supporter
Forum|alt.badge.img+38
  • Supporter
  • 592 replies
  • Best Answer
  • March 1, 2023

Hi.

Not really possible with the built-in PythonCaller (defined in fmesuite.py):

PARAMETER_NAME: PYTHONSOURCE
# note: not this is not or_attr because we do not want
# expression wrapping.  
PARAMETER_TYPE: OPTIONAL TEXT_EDIT FME_SYNTAX%PYTHON%FME_INLINE%YES
PARAMETER_PROMPT: Python Script:
PARAMETER_DEFAULT: import<space>fme<lf>import<space>fmeobjects<lf>...

You could build another version yourself with such a feature, but it sounds more like you want to have an external tool library (module) available for your Python code, and this is very possible.

Such modules need to be stored under either %FME_HOME%/Python or %APPDATA%/Roaming/Python, or at least in a folder included in the environment variable PYTHON_PATH.

Then just import it into your Python code.


oliver
Contributor
Forum|alt.badge.img+6
  • Contributor
  • 15 replies
  • March 1, 2023

To expand your PYTHON_PATH, you can use sys.path.append() as well.

But your QGIS Python should be the same Python version and bit version (32bit/64bit).


Forum|alt.badge.img+2
  • 1891 replies
  • March 1, 2023

@binaryannamolly​ Can you use the SystemCaller to run your python script?


david_r
Celebrity
  • 8394 replies
  • March 2, 2023

You can use a PythonCaller to call the other script, using something like:

import os
os.system("c:\my\other\folder\script2.py param1 param2")

 


You can use a PythonCaller to call the other script, using something like:

import os
os.system("c:\my\other\folder\script2.py param1 param2")

 

Thank you very much for the reply! I really appreciate it.

I need to play around with this. It looks like a great and elegant solution - but I did not manage to get it to work. I am sure it is just a simple mistake somewhere, so will try again with a fresh pair of eyes and a fresh mind.


@binaryannamolly​ Can you use the SystemCaller to run your python script?

That is a good idea! I should give it a try. What ended up working now was to just import the script - which immediately ran the script in question.

Not ideal as I then need to hard code the variables in it, but it works.


To expand your PYTHON_PATH, you can use sys.path.append() as well.

But your QGIS Python should be the same Python version and bit version (32bit/64bit).

Thank you very much for the reply! I got it working with by adding the script to the C:\\Users\\{username}\\Documents\\FME\\Plugins\\Python and importing the python script/module.


Hi.

Not really possible with the built-in PythonCaller (defined in fmesuite.py):

PARAMETER_NAME: PYTHONSOURCE
# note: not this is not or_attr because we do not want
# expression wrapping.  
PARAMETER_TYPE: OPTIONAL TEXT_EDIT FME_SYNTAX%PYTHON%FME_INLINE%YES
PARAMETER_PROMPT: Python Script:
PARAMETER_DEFAULT: import<space>fme<lf>import<space>fmeobjects<lf>...

You could build another version yourself with such a feature, but it sounds more like you want to have an external tool library (module) available for your Python code, and this is very possible.

Such modules need to be stored under either %FME_HOME%/Python or %APPDATA%/Roaming/Python, or at least in a folder included in the environment variable PYTHON_PATH.

Then just import it into your Python code.

Thank you very much for the reply! I got it working by adding the script to the C:\Users\{username}\Documents\FME\Plugins\Python and importing the script/module.

 

It is not ideal as the script now  ran immedialtely as it was imported - which means it used the hard coded variables in the script. I will look into changing this by making use of david_r's reply below.