I have a Global variable in a startup python which is used in the shutdown script but the Global variable is not recognised when the python is imported as external scripts.
Solved
I have a Global variable in a startup python which is used in the shutdown script but the Global variable is not recognised when the python is imported as external scripts.
Hi @takashi, I was using your method of importing an external startup & shutdown python scripts instead of typing them directly into the fme. However, I have a Global variable in the startup python which is used in the shutdown script but the Global variable is not recognised when the startup & shutdown python is imported as external scripts. eg.
import sys
sys.path.append('D:/Scripts')
import StartupPython
Best answer by david_r
In addition to the excellent replies from Takashi, I'll just add that this question touches on what's commonly called scope resolution in programming languages.
Hi @johnm, I think global variables defined in an external script file can also be used in Startup/Shutdown script in a workspace. Can you show us some code snippets to clarify the followings?
How did you define the global variable in "StartupPython.py"?
How did you use the variable in the Startup and/or Shutdown script in the workspace?
Hi @takashi I have attached the startup and shutdown scripts which would have a .py extension if imported in the Workspace. They originally came from your code when I was trying to grab all the Errors from the log file when the Workspace is running. They work well when they are copied in as text but when they are imported as .py files, the shutdown script fails to recognise the global 'errors' variable. Thanks again for your help on this. Regards, John
Each script file will be interpreted independently, Python interpreter cannot recognize where the variable "errors" come from, unless you import it explicitly. You have to import "startuppython" into "shutdownpython.py". For example, assuming both "startuppython.py" and "shutdownpython.py" are saved in the same folder,
Each script file will be interpreted independently, Python interpreter cannot recognize where the variable "errors" come from, unless you import it explicitly. You have to import "startuppython" into "shutdownpython.py". For example, assuming both "startuppython.py" and "shutdownpython.py" are saved in the same folder,
In addition to the excellent replies from Takashi, I'll just add that this question touches on what's commonly called scope resolution in programming languages.
Hi @takashi The import of the startup script into the shutdown script did not work for me. Maybe I will have to write the data from the errors variable to a text file in the startup script?
Hi @takashi The import of the startup script into the shutdown script did not work for me. Maybe I will have to write the data from the errors variable to a text file in the startup script?
How did you write the Startup and Shutdown scripts in the workspace to execute the external scripts?
global function to re-execute your main script's code. You will need to write a wrapper script that imports your main script, asks it for the variable it wants to cache, caches a copy of that within the wrapper script's module scope, and then when you want (when you hit ENTER on stdin or whatever), it calls
reload(yourscriptmodule)
but this time passes it the cached object such that yourscript can bypass the expensive computation.
We use 3 different kinds of cookies. You can choose which cookies you want to accept. We need basic cookies to make this site work, therefore these are the minimum you can select. Learn more about our cookies.