I'm calling a Login API and getting a token back. I want to store that token in an environment variable so that other jobs can use it.
The last statement in my Python 3 script is context.updateVariable('myvar', someval). Script succeeds in 0.4s, but when I look at the environment variables afterwards, nothing has changed.
Please help! What is the pattern most people are using for this situation?
Hi @greg.johnson
If there is an OR component after the python but before the variable is used again, it will reset to default. Also, the variable needs to be SHARED (that might be the default for environment variables anyway). I hope this is helpful.
Thank you,
Chika
Still not getting it. I'm attaching some screen grabs. Using Python 3... does that matter?
I've got a Login job that (using Python) successfully calls the Login routine, receives a token and issue the context.updateVariable() function shown in the screen grab. The variable mentioned is an Environment Variable, and it's shared.
I'm then expecting that second job to issue it's request.get() call using the environment variable token value I just set in the Login job. I can't imagine where I'm going off the rails here. Stick with me Chika... any ideas?
<one attachment at a time? really?>
Is it possible to set an environment variable in Python using context.updateVariable()?
Hi @greg.johnson
Let's see if this will be of assistance:
Set the job variables using this environment variable at the top level.
As for how to solve this specific use-case - if you do not want to manually set the environment variable - then you should use the token to make a second API call to

to set the environment value which can then be used/referenced within jobs in the specified Group Project.
Hope this helps.
Chika
Yessss.... thank you! This worked just fine.
So, if context.updateVariable() is NOT for updating environment variables programmatically like I was trying to do... what's it for? Job variables? How have I mis-used it?
Hi @greg.johnson
When you update the value of a variable via a Python Script component or are more specific components (Query Result to Scalar, Query Result to Grid), they update the current value of the related Variable, which might be a Job Variable, Environment Variable or Grid Variable (See the article on Variables for more details around Variables and the different types of variables).
The value that has been set for the variable will persist for the duration of the job, or until the variable value is set to a different value during the execution of the job. Related to this, the Behavior of the Variable will define if the value is thread-safe. With the above said, these methods will not permanently change the default value of a Variable. I think this might be the area they are focusing on. So, they can indeed use the context.updateVariable syntax in a Python Script to update the current value of an Environment Variable. Doing so will impact the value for the duration of the job execution.
But, when the job execution completes, any jobs that use that same Environment Variable will get the Default value of the Environment Variable, unless that job execution also has updated the value of the Environment Variable as part of its execution. If there is a desire to change the default value of an Environment Variable, that can be done by editing the Environment Variable via the Matillion UI or via Matillion’s API layer using the Environment API endpoint.
PS We have an upcoming Matillion Academy course on using Python in Matillion ETL and it will cover things like setting variable values via Python. Stay tuned for that!
Cheers!
Chika