# Reading device variable from python container

**URL:** <https://forums.balena.io/t/reading-device-variable-from-python-container/366498>\
**Category:** Project help\
**Created:** [January 24, 2023, 10:44am UTC](https://forums.balena.io/t/reading-device-variable-from-python-container/366498 "2023-01-24T10:44:30Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jesper](https://avatars.discourse-cdn.com/v4/letter/j/49beb7/32.png) [@Jesper](https://forums.balena.io/u/Jesper)\
**Post date:** [January 24, 2023, 10:44am UTC](https://forums.balena.io/t/reading-device-variable-from-python-container/366498/1 "2023-01-24T10:44:30Z")

</div>

Hi,

I have a service called “con” in witch I’m running my python code.  
How can I access my device variable called “DEVICE\_NAME”?

---

<div class="post-metadata">

**Author:** ![mpous](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/mpous/32/4742_2.png) [@mpous](https://forums.balena.io/u/mpous)\
**Post date:** [January 24, 2023, 12:03pm UTC](https://forums.balena.io/t/reading-device-variable-from-python-container/366498/2 "2023-01-24T12:03:45Z")

</div>

Hello @Jesper

Did you try to get the information from the balenaCloud API? [Resources - Balena Documentation](https://docs.balena.io/reference/api/resources/device/#device-by-id)

?

---

<div class="post-metadata">

**Author:** ![Jesper](https://avatars.discourse-cdn.com/v4/letter/j/49beb7/32.png) [@Jesper](https://forums.balena.io/u/Jesper)\
**Post date:** [January 24, 2023, 12:19pm UTC](https://forums.balena.io/t/reading-device-variable-from-python-container/366498/4 "2023-01-24T12:19:21Z")

</div>

@mpous  
I have looked at it, but then I would have to modify my code for each device, so that it would include the Device ID and auth token for use in the http call?

That seems a bit od.

---

<div class="post-metadata">

**Author:** ![mpous](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/mpous/32/4742_2.png) [@mpous](https://forums.balena.io/u/mpous)\
**Post date:** [January 24, 2023, 12:51pm UTC](https://forums.balena.io/t/reading-device-variable-from-python-container/366498/5 "2023-01-24T12:51:15Z")

</div>

@Jesper did you try to get the device UUID using these variables?

> **[Communicate outside the container - Balena Documentation](https://docs.balena.io/learn/develop/runtime/#environment-variables)**
>
> Talk to the host OS, supervisor, and network from within a balena container

you might use `os.environ['BALENA_DEVICE_UUID']` and then you might get the name. Here you have an example from a bash script and not to get the name but similar:

> <https://github.com/mpous/basicstation/blob/04a95fef320c46ff4cf89004edd543653d35dac6/start.sh#L16>

Let us know if this works!

---

<div class="post-metadata">

**Author:** ![mcraa](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/mcraa/32/6648_2.png) [@mcraa](https://forums.balena.io/u/mcraa)\
**Post date:** [January 24, 2023, 12:58pm UTC](https://forums.balena.io/t/reading-device-variable-from-python-container/366498/6 "2023-01-24T12:58:35Z")

</div>

If you can run `printenv` in the container you see what env vars are available.  
I found `BALENA_DEVICE_NAME_AT_INIT` closest to Device name, i hope that suffices your needs.

---

<div class="post-metadata">

**Author:** ![Jesper](https://avatars.discourse-cdn.com/v4/letter/j/49beb7/32.png) [@Jesper](https://forums.balena.io/u/Jesper)\
**Post date:** [January 24, 2023, 1:09pm UTC](https://forums.balena.io/t/reading-device-variable-from-python-container/366498/7 "2023-01-24T13:09:06Z")

</div>

It does!!  
In the forum post I found they did not mention this:

```auto
environment:
      - 'DBUS_SYSTEM_BUS_ADDRESS=unix:path=/host/run/dbus/system_bus_socket'

```

So now I can get the info using eg. os.environ[‘BALENA\_DEVICE\_UUID’]
