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?


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.

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

8 replies

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

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


david_r
Celebrity
  • 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")

 


david_r wrote:

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.


markatsafe wrote:

@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.


oliver wrote:

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.


lifalin2016 wrote:

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.


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