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?

icon

Best answer by lifalin2016 1 March 2023, 14:27

View original

8 replies

Userlevel 1
Badge +22

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.

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

Badge +2

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

Userlevel 4

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.

Reply