Changing Network at Runtime in Python3.8+

I am trying to use the following snippet to re-connect to the wifi:

I am using the balenalib/raspberrypi3-python:3.8-buster-20200518 image and I am installing the python dbus module through python3-dbus package. It seems though that it requires Python 3.7 as it installs python3.7-minimal and when trying to import dbus in python I am getting ModuleNotFoundError: No module named '_dbus_bindings' error.
Has anyone managed to use python3-dbus in Python 3.8+?

Any thoughts would be appreciated.

PS: everything works as expected in Python 3.7.


the Python 3.8 within the image is distinct from the system-wide Python provided by Debian so installing system packages will have no effect on it. You should install Python 3.8 modules using pip. As in your particular case dbus-python is compiled on install it is best to do this in a multi-stage build, the Dockerfile snippet can look like this:

FROM balenalib/raspberrypi3-python:3.8-buster-build-20200518 as build
RUN pip install dbus-python

FROM balenalib/raspberrypi3-python:3.8-buster-20200518
COPY --from=build /usr/local/lib/python3.8/site-packages/ /usr/local/lib/python3.8/

... the rest of your dockerfile

Thx @mtoman! Your solution worked.
I have tried in the past to pip install dbus-python from within the container but the compilation was failing.