Installing on Linux under Pyenv

My OS is MX Linux 23, based on Debian 12, the default Python 3 version is 3.11.2. I installed the package python3-wxgtk4.0 through the package manager, everything worked fine. The version is ‘4.2.0 gtk3 (phoenix) wxWidgets 3.2.2’.

I have additionally installed Python 3.13.12 with pyenv and tried installing the latest version of wxPython there with pip install wxPython - failed at first, but then sort of succeeded, after installing the system libgtk-3-dev package. pip install wxPython was doing something for about 20 minutes (building from the sources, I assume) and then reported a successful installation of wxPython-4.2.5.

However, attempting to import wx gives the following error:

File "/home/<username>/bin/pyenv/versions/3.13.12/lib/python3.13/site-packages/wx/__init__.py", line 17, in <module>  
   from wx.core import *  
 File "/home/<username>/bin/pyenv/versions/3.13.12/lib/python3.13/site-packages/wx/core.py", line 12, in <module>  
   from ._core import *  
ImportError: /lib/x86_64-linux-gnu/libwx_baseu-3.2.so.0: version `WXU_3.2.6' not found (required by /home/<username>/bin/pyenv/versions/3.1  
3.12/lib/python3.13/site-packages/wx/_core.cpython-313-x86_64-linux-gnu.so)

There’s another file with that name: .../pyenv/versions/3.13.12/lib/python3.13/site-packages/wx/libwx_baseu-3.2.so.0 that was created during the installation and, I assume, is of the required version. How do I tell wxPython to use it instead of the one from /lib/...?

I don’t know if this is more about Pyenv than wxPython, but I’ve installed a few other modules that are not pure Python (i. e. also have binary libraries in their directories), and they seem to work fine.

I might have solved it - not sure if completely, or in the most sensible way, but my simple test GUI works, wxPython-demo-4.2.5 also mostly works, except some samples that require additional libraries I currently don’t have installed in the environment (cairo, pdf, etc.).

To run from the terminal

export LD_LIBRARY_PATH="<path to pyenv>/versions/3.13.12/lib/python3.13/site-packages/wx"
(append with :$LD_LIBRARY_PATH if you must, add to .bashrc or whatever), then it can be ran with python myscript.py or ./myscript.py if it has the permission and the shebang pointing to the correct interpreter.

To run without the terminal

i.e. by double-clicking in the file manager, etc. I’ve found two ways.

1. Patching the libraries

(sudo apt install patchelf)
From within the directory where the library resides (<path to pyenv>/versions/3.13.12/lib/python3.13/site-packages/wx)
do:
patchelf --set-rpath '$ORIGIN' _core.cpython-313-x86_64-linux-gnu.so
This will patch the library, making it look for dependencies in the same folder. This was enough to make everything (?) work.

2. Creating symlinks to the libraries

Without patching, the library searches for its dependencies in <path to pyenv>/versions/3.13.12/lib (can be checked with the same patchelf tool as patchelf --print-rpath _core.cpython-313-x86_64-linux-gnu.so). The library is not there, so it defaults (I guess?) to the system-wide location (/lib/...). There are these libraries in .../site-packages/wx: libwx_baseu-3.2.so.0 and libwx_gtk3u_core-3.2.so.0, create symlinks to them in <path to pyenv>/versions/3.13.12/lib. This was enough to make everything (?) work.

Final thoughts

There are other libraries like that in .../wx - maybe the others like _adv.*, _aui.*, etc. would also require patching, or the others like libwx_* would also require the symlinks, but so far everything seem to work without that, including wx.adv, wx.aui, etc.

I kinda feel like there should be an installation or configuration option making all of the above unnecessary, but so far I haven’t found anything.