Confusion about external/system Python & libraries

I’m really confused about how to deal with external Python dependencies in my game - so far I’ve resigned myself to copying the relevant libraries to the location of the .blend file, and this has worked well. But now I need a Python library that is a binary installed with apt-get (python3-smbus), and doesn’t produce a directory or file in dist/site-packages that I can copy. I’ve tried installing Python 3.7 (which I had to build) alongside the system default of 3.9 and switching between them with update-alternatives, but even after apt-get install --reinstall python3-smbus with 3.7 as the system Python - and setting the PYTHONPATH to include the location of 3.7 (/usr/local/lib/python3.7) I still get the same error when launching my game:

Traceback (most recent call last):
  File "<string>", line 6, in <module>
  File "/home/station/station.py", line 13, in <module>
    from fadeleds import FadeLEDs
  File "/home/station/fadeleds.py", line 3, in <module>
    import sn3218, time, math
  File "/home/station/sn3218.py", line 31, in <module>
    _raise_from_none(import_error)
  File "<string>", line 1, in _raise_from_none
ImportError: This library requires python3-smbus
Install with: sudo apt install python3-smbus
Debug: Exit Python script 'main.py'

How can I help blenderplayer find this library?

P.S. I’m not talking about a bundled game engine runtime here; my “game” will always be run with blenderplayer and the .blend file.

Maybe you should try installing the package using pip from Blender’s Python itself. Try installing pip on your Blender:

cd '/path/to/blender/2.79/python/bin/'
./python3.7m -m ensurepip
./pip3.7 install --upgrade pip

Then you can install the library on your Blender:

./pip3.7 install my_library

However I couldn’t install this library smbus in my Blender, it says error: legacy-install-failure while building, but that’s a start.

1 Like

That’s what I do on my main machine, which has the full UPBGE 0.2.5 installation. But once the modeling and programming has been done the .blend is copied to a Raspberry Pi 4 which will be running it with blenderplayer. I had to compile this from source since there are no official aarch64 binaries, and it’s only blenderplayer (49 MB) and nothing else; no Blender, no Python, no Pip.

Screenshot_2022-06-20_12-42-30

God knows where it gets its Python from - it’s not using the system’s 3.9 so I guess it must be bundled inside blenderplayer itself? In any case, it works perfectly fine with standalone libraries like six.py, pySerial and pyTransitions, provided these are in the same location as the .blend.

Ok, this problem gets far worse when I try to use the RPi.GPIO library - which is kind of the whole point with running UPBGE on a Pi. I’m gonna need some help here! Please?

Traceback (most recent call last):
  File "<string>", line 7, in <module>
  File "/home/station/station.py", line 211, in <module>
    station = Station() # RIP Mr Jones
  File "/home/station/station.py", line 101, in __init__
    self.btnled = FadeLEDs(self.led_queue)
  File "/home/station/fadeleds.py", line 14, in __init__
    sn3218.reset()
  File "/home/station/sn3218/__init__.py", line 153, in reset
    _get_sn3218().reset()
  File "/home/station/sn3218/__init__.py", line 128, in _get_sn3218
    _sn3218 = SN3218()
  File "/home/station/sn3218/__init__.py", line 20, in __init__
    import RPi.GPIO as GPIO
  File "/home/station/RPi/GPIO/__init__.py", line 23, in <module>
    from RPi._GPIO import *
ModuleNotFoundError: No module named 'RPi._GPIO'

This is after copying /usr/lib/python3/dist-packages/RPi to where the .blend file lives. You can see it finds the RPi.GPIO library, but fails when it tries to load RPi._GPIO, which is the binary file _GPIO.cpython-39-aarch64-linux-gnu.so

the python version of the module needs to be the same as the version of python your install of UPBGE uses, otherwise it wont load.

it look like the version of your CPIO is 3.9 and i think UPBGE 2.5 use version 3.7 if im not mistaken

1 Like

Understood. So if I set up a separate installation with system Python 3.7 and install the RPi.GPIO package I should be able to use this if I copy it to the .blend location?

check your UPBGE install for the correct version, the 3.7 number comes from the top of my head, could be wrong

Pretty sure you’re right, that UPBGE 0.2.5 uses Python 3.7. I’m going to try this again.

Yep, after adding 3.7 as an alternative system Python (which luckily I already had a compiled version of for aarch64), switching to this with update-alternatives (this may not have been necessary, tbh) and installing RPi.GPIO with sudo pip3.7 install RPi.GPIO, then copying the resulting library from /usr/local/lib/python3.7/site-packages/RPi to where the .blend file is, blenderplayer can now use RPi.GPIO. Phew. And it works too - I can now control a SN3218 LED driver via i2c from within my .blend :upside_down_face: Thanks spiderman!

1 Like