# Python SDK: Creating/Updating/Removing environment variables (from device itself)

**URL:** <https://forums.balena.io/t/python-sdk-creating-updating-removing-environment-variables-from-device-itself/2728>\
**Category:** Product support\
**Created:** [March 15, 2018, 9:13pm UTC](https://forums.balena.io/t/python-sdk-creating-updating-removing-environment-variables-from-device-itself/2728 "2018-03-15T21:13:26Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![FransvanHoogstraten](https://avatars.discourse-cdn.com/v4/letter/f/85e7bf/32.png) [@FransvanHoogstraten](https://forums.balena.io/u/FransvanHoogstraten)\
**Post date:** [March 15, 2018, 9:13pm UTC](https://forums.balena.io/t/python-sdk-creating-updating-removing-environment-variables-from-device-itself/2728/1 "2018-03-15T21:13:26Z")

</div>

Hi guys,

Maybe you can help with something:

My devices use the Python SDK to set the environment variables they need themselves. After downloading their individual configuration from our backend they use the “resin.models.environment\_variables.device.create” function to create the needed variables.

Now, that works fine. But somehow, updating and removing env vars results in errors:

When updating a variable via “resin.models.environment\_variables.device.update()” I get this:

```
15.03.18 21:57:55 (+0100) Now updating config_var 'RESIN_HOST_CONFIG_disable_splash' on device to value '0'
15.03.18 21:57:56 (+0100) Exception in thread Thread-3:
15.03.18 21:57:56 (+0100) Traceback (most recent call last):
15.03.18 21:57:56 (+0100) File "/usr/local/lib/python2.7/threading.py", line 801, in __bootstrap_inner
15.03.18 21:57:56 (+0100) self.run()
15.03.18 21:57:56 (+0100) File "/usr/local/lib/python2.7/threading.py", line 754, in run
15.03.18 21:57:56 (+0100) self.__target(*self.__args, **self.__kwargs)
15.03.18 21:57:56 (+0100) File "ResinManager.py", line 142, in set_device_environment_variables_thread
15.03.18 21:57:56 (+0100) config_var_dict[entry])
15.03.18 21:57:56 (+0100) File "/usr/local/lib/python2.7/site-packages/resin/models/environment_variables.py", line 128, in update
15.03.18 21:57:56 (+0100) endpoint=self.settings.get('pine_endpoint')
15.03.18 21:57:56 (+0100) File "/usr/local/lib/python2.7/site-packages/resin/base_request.py", line 175, in request
15.03.18 21:57:56 (+0100) raise exceptions.RequestError(response._content)
15.03.18 21:57:56 (+0100) RequestError

```

And when removing a variable via “resin.models.environment\_variables.device.remove()” I get this:

```
15.03.18 22:04:06 (+0100) Now removing config_var 'RESIN_HOST_CONFIG_gpu_mem' on device with value '16' and id '131778'
15.03.18 22:04:07 (+0100) Exception in thread Thread-3:
15.03.18 22:04:07 (+0100) Traceback (most recent call last):
15.03.18 22:04:07 (+0100) File "/usr/local/lib/python2.7/threading.py", line 801, in __bootstrap_inner
15.03.18 22:04:07 (+0100) self.run()
15.03.18 22:04:07 (+0100) File "/usr/local/lib/python2.7/threading.py", line 754, in run
15.03.18 22:04:07 (+0100) self.__target(*self.__args, **self.__kwargs)
15.03.18 22:04:07 (+0100) File "ResinManager.py", line 154, in set_device_environment_variables_thread
15.03.18 22:04:07 (+0100) resin.models.environment_variables.device.remove(current_vars_on_device_dict[entry][0])
15.03.18 22:04:07 (+0100) File "/usr/local/lib/python2.7/site-packages/resin/models/environment_variables.py", line 150, in remove
15.03.18 22:04:07 (+0100) endpoint=self.settings.get('pine_endpoint')
15.03.18 22:04:07 (+0100) File "/usr/local/lib/python2.7/site-packages/resin/base_request.py", line 175, in request
15.03.18 22:04:07 (+0100) raise exceptions.RequestError(response._content)
15.03.18 22:04:07 (+0100) RequestError

```

This can’t be expected behaviour right?

Thanks!

Frans

---

<div class="post-metadata">

**Author:** ![lucianbuzzo](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/lucianbuzzo/32/615_2.png) [@lucianbuzzo](https://forums.balena.io/u/lucianbuzzo)\
**Post date:** [March 21, 2018, 1:17pm UTC](https://forums.balena.io/t/python-sdk-creating-updating-removing-environment-variables-from-device-itself/2728/9 "2018-03-21T13:17:30Z")

</div>

Hi, We’ve recently released v3 of the python SDK which appears to fix the issues you’re seeing. Could you try the new version and see if that fixes your issue?

---

<div class="post-metadata">

**Author:** ![FransvanHoogstraten](https://avatars.discourse-cdn.com/v4/letter/f/85e7bf/32.png) [@FransvanHoogstraten](https://forums.balena.io/u/FransvanHoogstraten)\
**Post date:** [March 21, 2018, 3:39pm UTC](https://forums.balena.io/t/python-sdk-creating-updating-removing-environment-variables-from-device-itself/2728/13 "2018-03-21T15:39:15Z")

</div>

Thanks guys, I will try!

---

<div class="post-metadata">

**Author:** ![FransvanHoogstraten](https://avatars.discourse-cdn.com/v4/letter/f/85e7bf/32.png) [@FransvanHoogstraten](https://forums.balena.io/u/FransvanHoogstraten)\
**Post date:** [March 21, 2018, 4:24pm UTC](https://forums.balena.io/t/python-sdk-creating-updating-removing-environment-variables-from-device-itself/2728/14 "2018-03-21T16:24:34Z")

</div>

Hmm, I’m afraid with ‘remove’ I still get this:

```
21.03.18 17:13:39 (+0100) main Exception in thread Thread-3:
21.03.18 17:13:39 (+0100) main Traceback (most recent call last):
21.03.18 17:13:39 (+0100) main File "/usr/local/lib/python2.7/threading.py", line 801, in __bootstrap_inner
21.03.18 17:13:39 (+0100) main self.run()
21.03.18 17:13:39 (+0100) main File "/usr/local/lib/python2.7/threading.py", line 754, in run
21.03.18 17:13:39 (+0100) main self.__target(*self.__args, **self.__kwargs)
21.03.18 17:13:39 (+0100) main File "ResinManager.py", line 154, in set_device_environment_variables_thread
21.03.18 17:13:39 (+0100) main resin.models.environment_variables.device.remove(current_vars_on_device_dict[entry][0])
21.03.18 17:13:39 (+0100) main File "/usr/local/lib/python2.7/site-packages/resin/models/environment_variables.py", line 150, in remove
21.03.18 17:13:39 (+0100) main endpoint=self.settings.get('pine_endpoint')
21.03.18 17:13:39 (+0100) main File "/usr/local/lib/python2.7/site-packages/resin/base_request.py", line 175, in request
21.03.18 17:13:39 (+0100) main raise exceptions.RequestError(response._content)
21.03.18 17:13:39 (+0100) main RequestError

```

And with update I get this:

```
Exception in thread Thread-3:
21.03.18 17:16:39 (+0100) main Traceback (most recent call last):
21.03.18 17:16:39 (+0100) main File "/usr/local/lib/python2.7/threading.py", line 801, in __bootstrap_inner
21.03.18 17:16:39 (+0100) main self.run()
21.03.18 17:16:39 (+0100) main File "/usr/local/lib/python2.7/threading.py", line 754, in run
21.03.18 17:16:39 (+0100) main self.__target(*self.__args, **self.__kwargs)
21.03.18 17:16:39 (+0100) main File "ResinManager.py", line 142, in set_device_environment_variables_thread
21.03.18 17:16:39 (+0100) main config_var_dict[entry])
21.03.18 17:16:39 (+0100) main File "/usr/local/lib/python2.7/site-packages/resin/models/environment_variables.py", line 128, in update
21.03.18 17:16:39 (+0100) main endpoint=self.settings.get('pine_endpoint')
21.03.18 17:16:39 (+0100) main File "/usr/local/lib/python2.7/site-packages/resin/base_request.py", line 175, in request
21.03.18 17:16:39 (+0100) main raise exceptions.RequestError(response._content)
21.03.18 17:16:39 (+0100) main RequestError

```

So what would be needed to debug this? Shall I paste here the code that sets the env vars? Or would you care to login to one of the devices with this behaviour?

---

<div class="post-metadata">

**Author:** ![nghiant2710](https://avatars.discourse-cdn.com/v4/letter/n/90db22/32.png) [@nghiant2710](https://forums.balena.io/u/nghiant2710)\
**Post date:** [March 21, 2018, 5:38pm UTC](https://forums.balena.io/t/python-sdk-creating-updating-removing-environment-variables-from-device-itself/2728/15 "2018-03-21T17:38:52Z")

</div>

Is this a single container or multicontainer device? We’ve not supported multicontainer yet so it might now work properly. We will release Python SDK v4 which supports multicontainer really soon.

---

<div class="post-metadata">

**Author:** ![FransvanHoogstraten](https://avatars.discourse-cdn.com/v4/letter/f/85e7bf/32.png) [@FransvanHoogstraten](https://forums.balena.io/u/FransvanHoogstraten)\
**Post date:** [March 21, 2018, 7:59pm UTC](https://forums.balena.io/t/python-sdk-creating-updating-removing-environment-variables-from-device-itself/2728/17 "2018-03-21T19:59:42Z")

</div>

Single container!

---

<div class="post-metadata">

**Author:** ![ArkTobe](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/arktobe/32/2854_2.png) [@ArkTobe](https://forums.balena.io/u/ArkTobe)\
**Post date:** [December 5, 2019, 5:45pm UTC](https://forums.balena.io/t/python-sdk-creating-updating-removing-environment-variables-from-device-itself/2728/19 "2019-12-05T17:45:36Z")

</div>

About the same issue here with docker build arguments variables not passing through a common python3 script. It wouldn’t be able to parse the passed arguments from Bash she’ll script. I’m afraid!

---

<div class="post-metadata">

**Author:** ![thgreasi](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/thgreasi/32/454_2.png) [@thgreasi](https://forums.balena.io/u/thgreasi)\
**Post date:** [December 5, 2019, 5:52pm UTC](https://forums.balena.io/t/python-sdk-creating-updating-removing-environment-variables-from-device-itself/2728/20 "2019-12-05T17:52:58Z")

</div>

Hi @ArkTobe  
Can you also clarify what version of the python sdk are you using?

---

<div class="post-metadata">

**Author:** ![thgreasi](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/thgreasi/32/454_2.png) [@thgreasi](https://forums.balena.io/u/thgreasi)\
**Post date:** [December 5, 2019, 6:23pm UTC](https://forums.balena.io/t/python-sdk-creating-updating-removing-environment-variables-from-device-itself/2728/21 "2019-12-05T18:23:48Z")

</div>

I think that I might be missing something.  
Can provide a bit more info about what you are trying to achieve?  
If you are using build time variables with `ARG`, these are not going to be available in the container running on the device.  
If you on the other hand use the docker-compose.yml `environment:` to define variables, then those will be available in the containers on run-time.

---

<div class="post-metadata">

**Author:** ![ArkTobe](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/arktobe/32/2854_2.png) [@ArkTobe](https://forums.balena.io/u/ArkTobe)\
**Post date:** [January 14, 2020, 10:32pm UTC](https://forums.balena.io/t/python-sdk-creating-updating-removing-environment-variables-from-device-itself/2728/24 "2020-01-14T22:32:29Z")

</div>

Settings tru dashboard is ok in bash shell. Going through Python scripts with environment variables set isn’t straight forward is it. You can only inject scripts run through `RUN ["bash", "-c ","python3 $ENVIRONMENT_PARAMETER"] `, by doing that so, is getting well, finally,. This is not very useful since the output isn’t easily redirected to the standard error.  
I have set my template ARG as ENV entries to make them available to further commands, that’s all it can do at build time at the moment. Eventually, all runtime values need to be set through docker-compose.yml `env_file: afile.env `or dashboard  
To be clear, AFAIK security issues may arise if we could use environment variables in Python scripted files, it isn’t safe to produce scripts with access to the environment settings on-the-go.
