OpenBB icon indicating copy to clipboard operation
OpenBB copied to clipboard

[Bug] matplotlib failed to import any qt binding with X-Server on linux

Open dvinella opened this issue 3 years ago • 9 comments

Describe the bug Following the docker advanced settings documentation to start a local container with X-Server support on linux, an error is triggered by matplotlib, unable to find any QT binding

To Reproduce

$ cat env 
OPENBB_BACKEND=Qt5Agg
$ xhost +local:
non-network local connections being added to access control list
$ podman run -it --rm --name openbb --env-file=./env -e DISPLAY=$DISPLAY -v /tmp/.X11-unix:/tmp/.X11-unix ghcr.io/openbb-finance/openbbterminal-poetry:latest
Traceback (most recent call last):
  File "/home/python/terminal.py", line 20, in <module>
    from openbb_terminal.common import feedparser_view
  File "/home/python/openbb_terminal/common/feedparser_view.py", line 8, in <module>
    from openbb_terminal.helper_funcs import export_data
  File "/home/python/openbb_terminal/helper_funcs.py", line 45, in <module>
    matplotlib.use(cfgPlot.BACKEND)
  File "/home/python/.cache/pypoetry/virtualenvs/openbbterminal-K8M3siuK-py3.9/lib/python3.9/site-packages/matplotlib/__init__.py", line 1144, in use
    plt.switch_backend(name)
  File "/home/python/.cache/pypoetry/virtualenvs/openbbterminal-K8M3siuK-py3.9/lib/python3.9/site-packages/matplotlib/pyplot.py", line 288, in switch_backend
    class backend_mod(matplotlib.backend_bases._Backend):
  File "/home/python/.cache/pypoetry/virtualenvs/openbbterminal-K8M3siuK-py3.9/lib/python3.9/site-packages/matplotlib/pyplot.py", line 289, in backend_mod
    locals().update(vars(importlib.import_module(backend_name)))
  File "/usr/local/lib/python3.9/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "/home/python/.cache/pypoetry/virtualenvs/openbbterminal-K8M3siuK-py3.9/lib/python3.9/site-packages/matplotlib/backends/backend_qt5agg.py", line 7, in <module>
    from .backend_qtagg import (    # noqa: F401, E402 # pylint: disable=W0611
  File "/home/python/.cache/pypoetry/virtualenvs/openbbterminal-K8M3siuK-py3.9/lib/python3.9/site-packages/matplotlib/backends/backend_qtagg.py", line 9, in <module>
    from .qt_compat import QT_API, _enum, _setDevicePixelRatio
  File "/home/python/.cache/pypoetry/virtualenvs/openbbterminal-K8M3siuK-py3.9/lib/python3.9/site-packages/matplotlib/backends/qt_compat.py", line 142, in <module>
    raise ImportError("Failed to import any qt binding")
ImportError: Failed to import any qt binding

Desktop (please complete the following information):

  • OS: Podman on ArchLinux

dvinella avatar Aug 06 '22 23:08 dvinella

Hello,

Can you try setting this to None instead of Qt5Agg?

$ cat env 
OPENBB_BACKEND=Qt5Agg

deeleeramone avatar Aug 08 '22 19:08 deeleeramone

Yes, with None as backend the container starts normally, but it is unable to draw any figure

/stocks/ $ candle
/home/python/openbb_terminal/helper_classes.py:373: UserWarning: Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure.
  plt.show()

dvinella avatar Aug 08 '22 20:08 dvinella

Have you followed these steps to set your display in your ENV file?

Users familiar with Docker and X-Server can set the DISPLAY variable in the file setenv described above. If you use this approach remember to add :0 at the end of your inet address. E.g. DISPLAY=192.168.1.155:0.

IP=$(ifconfig | grep inet | grep -v -e "127.0.0.1" | awk '$1=="inet" {print $2}')
xhost + $IP

https://github.com/OpenBB-finance/OpenBBTerminal/blob/main/DOCKER_ADVANCED.md#adding-the-display-for-docker

deeleeramone avatar Aug 08 '22 20:08 deeleeramone

For what I can understand those instructions are for MacOS, on linux I should use the local IPC socket. However, even trying using the ip connection I get the same error. I think the OPENBB_BACKEND=Qt5Agg is the correct setting, but something is missing in the container, maybe the qt bindings.

dvinella avatar Aug 11 '22 18:08 dvinella

@deeleeramone

Hello,

Can you try setting this to None instead of Qt5Agg?

$ cat env 
OPENBB_BACKEND=Qt5Agg

I was able to start the container and draw the images with this.

Sudeepa14 avatar Aug 13 '22 20:08 Sudeepa14

I'm hit by this, too.

I tried to solve it by:

diff --git a/docker/openbbterminal-python.dockerfile b/docker/openbbterminal-python.dockerfile
index 78ca12c59..e84de98a4 100644
--- a/docker/openbbterminal-python.dockerfile
+++ b/docker/openbbterminal-python.dockerfile
@@ -21,6 +21,7 @@ RUN  apt-get update && apt-get -y install --no-install-recommends \
   libgl1-mesa-glx \
   libpng16-16 \
   procps \
+  qt5-default \
   python3-tk && \
   curl -sL https://deb.nodesource.com/setup_16.x -o nodesource_setup.sh && \
   bash nodesource_setup.sh && \
diff --git a/requirements.txt b/requirements.txt
index d2171848f..85bd93125 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -172,6 +172,7 @@ pyotp==2.6.0; python_version >= "3"
 pyparsing==3.0.9; python_full_version >= "3.6.8" and python_version >= "3.7"
 pyportfolioopt==1.5.3; python_version >= "3.8" and python_version < "3.11"
 pyprind==2.11.3; python_version >= "2.7" and python_full_version < "3.0.0" or python_full_version >= "3.6.0"
+pyqt5==5.15.0
 pyrsistent==0.18.1; python_version >= "3.7"
 pysocks==1.7.1; python_version >= "3.7" and python_full_version < "3.0.0" and python_version < "4" or python_full_version >= "3.6.0" and python_version < "4" and python_version >= "3.7"
 python-binance==1.0.16

and rebuilding the docker image by:

cd docker
source compose.env
docker-compose build

but that didn't work for me either (I don't speak Python so that may very well be the reason 😉 ).

When these are set in the env file:

OPENBB_BACKEND=Qt5Agg
QT_API=pyqt5

I get this error:

Traceback (most recent call last):
  File "/home/python/terminal.py", line 21, in <module>
    from openbb_terminal.common import feedparser_view
  File "/home/python/openbb_terminal/common/feedparser_view.py", line 8, in <module>
    from openbb_terminal.helper_funcs import export_data
  File "/home/python/openbb_terminal/helper_funcs.py", line 45, in <module>
    matplotlib.use(cfgPlot.BACKEND)
  File "/home/python/.cache/pypoetry/virtualenvs/openbbterminal-K8M3siuK-py3.9/lib/python3.9/site-packages/matplotlib/__init__.py", line 1146, in use
    plt.switch_backend(name)
  File "/home/python/.cache/pypoetry/virtualenvs/openbbterminal-K8M3siuK-py3.9/lib/python3.9/site-packages/matplotlib/pyplot.py", line 282, in switch_backend
    class backend_mod(matplotlib.backend_bases._Backend):
  File "/home/python/.cache/pypoetry/virtualenvs/openbbterminal-K8M3siuK-py3.9/lib/python3.9/site-packages/matplotlib/pyplot.py", line 283, in backend_mod
    locals().update(vars(importlib.import_module(backend_name)))
  File "/usr/local/lib/python3.9/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "/home/python/.cache/pypoetry/virtualenvs/openbbterminal-K8M3siuK-py3.9/lib/python3.9/site-packages/matplotlib/backends/backend_qt5agg.py", line 7, in <module>
    from .backend_qtagg import (    # noqa: F401, E402 # pylint: disable=W0611
  File "/home/python/.cache/pypoetry/virtualenvs/openbbterminal-K8M3siuK-py3.9/lib/python3.9/site-packages/matplotlib/backends/backend_qtagg.py", line 9, in <module>
    from .qt_compat import QT_API, _enum, _setDevicePixelRatio
  File "/home/python/.cache/pypoetry/virtualenvs/openbbterminal-K8M3siuK-py3.9/lib/python3.9/site-packages/matplotlib/backends/qt_compat.py", line 120, in <module>
    _setup_pyqt5plus()
  File "/home/python/.cache/pypoetry/virtualenvs/openbbterminal-K8M3siuK-py3.9/lib/python3.9/site-packages/matplotlib/backends/qt_compat.py", line 102, in _setup_pyqt5plus
    from PyQt5 import QtCore, QtGui, QtWidgets
ModuleNotFoundError: No module named 'PyQt5'

when I only have this in the env file:

OPENBB_BACKEND=Qt5Agg

I get this error:

Traceback (most recent call last):
  File "/home/python/terminal.py", line 21, in <module>
    from openbb_terminal.common import feedparser_view
  File "/home/python/openbb_terminal/common/feedparser_view.py", line 8, in <module>
    from openbb_terminal.helper_funcs import export_data
  File "/home/python/openbb_terminal/helper_funcs.py", line 45, in <module>
    matplotlib.use(cfgPlot.BACKEND)
  File "/home/python/.cache/pypoetry/virtualenvs/openbbterminal-K8M3siuK-py3.9/lib/python3.9/site-packages/matplotlib/__init__.py", line 1146, in use
    plt.switch_backend(name)
  File "/home/python/.cache/pypoetry/virtualenvs/openbbterminal-K8M3siuK-py3.9/lib/python3.9/site-packages/matplotlib/pyplot.py", line 282, in switch_backend
    class backend_mod(matplotlib.backend_bases._Backend):
  File "/home/python/.cache/pypoetry/virtualenvs/openbbterminal-K8M3siuK-py3.9/lib/python3.9/site-packages/matplotlib/pyplot.py", line 283, in backend_mod
    locals().update(vars(importlib.import_module(backend_name)))
  File "/usr/local/lib/python3.9/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "/home/python/.cache/pypoetry/virtualenvs/openbbterminal-K8M3siuK-py3.9/lib/python3.9/site-packages/matplotlib/backends/backend_qt5agg.py", line 7, in <module>
    from .backend_qtagg import (    # noqa: F401, E402 # pylint: disable=W0611
  File "/home/python/.cache/pypoetry/virtualenvs/openbbterminal-K8M3siuK-py3.9/lib/python3.9/site-packages/matplotlib/backends/backend_qtagg.py", line 9, in <module>
    from .qt_compat import QT_API, _enum, _setDevicePixelRatio
  File "/home/python/.cache/pypoetry/virtualenvs/openbbterminal-K8M3siuK-py3.9/lib/python3.9/site-packages/matplotlib/backends/qt_compat.py", line 142, in <module>
    raise ImportError("Failed to import any qt binding")
ImportError: Failed to import any qt binding

My system is Fedora 36:

uname -a                                                                        
Linux  5.18.18-200.fc36.x86_64 #1 SMP PREEMPT_DYNAMIC Wed Aug 17 16:02:04 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux

metlos avatar Aug 24 '22 11:08 metlos

How about this backend?

OPENBB_BACKEND='TKAgg'

deeleeramone avatar Aug 24 '22 13:08 deeleeramone

With OPENBB_BACKEND=TKAgg I get:

Traceback (most recent call last):
  File "/home/python/terminal.py", line 21, in <module>
    from openbb_terminal.common import feedparser_view
  File "/home/python/openbb_terminal/common/feedparser_view.py", line 8, in <module>
    from openbb_terminal.helper_funcs import export_data
  File "/home/python/openbb_terminal/helper_funcs.py", line 45, in <module>
    matplotlib.use(cfgPlot.BACKEND)
  File "/home/python/.cache/pypoetry/virtualenvs/openbbterminal-K8M3siuK-py3.9/lib/python3.9/site-packages/matplotlib/__init__.py", line 1146, in use
    plt.switch_backend(name)
  File "/home/python/.cache/pypoetry/virtualenvs/openbbterminal-K8M3siuK-py3.9/lib/python3.9/site-packages/matplotlib/pyplot.py", line 290, in switch_backend
    raise ImportError(
ImportError: Cannot load backend 'TkAgg' which requires the 'tk' interactive framework, as 'headless' is currently running

metlos avatar Aug 29 '22 11:08 metlos

Ok, some progress here.

I noticed SELinux prevented access of Python running in the container to the Xorg socket file. So when I modified my local policies to allow that:

# ausearch -c 'python' --raw | audit2allow -M openbb-python
# semodule -X 300 -i openbb-python.pp

I was able to run the OpenBB terminal from docker with OPENBB_BACKEND=TkAgg. Still no luck with Qt though...

metlos avatar Aug 29 '22 14:08 metlos

I also hit the qt bug mentioned by @dvinella

RCasatta avatar Oct 29 '22 12:10 RCasatta