Could not find or load the Qt platform plugin "xcb" - python

On my OS(Linux Mint Debian Edition 2), except for the system python(/usr/bin/python) installed by the apt, I also installed the anaconda. But I've encounterd a problem running the following code with the anaconda python
# test.py
import matplotlib.pyplot as plt
import numpy as np
x = np.array([0, 1])
plt.scatter(x, x)
plt.show()
The error is
This application failed to start because it could not find or load the
Qt platform plugin "xcb".
Reinstalling the application may fix this problem.
Aborted
But if I try with the system python, i.e., /usr/bin/python test.py, it works correctly.
Then I tried the ipythons, of system and of anaconda, the result is same as before: the anaconda ipython kernel died.
And I tried add the ipython magic %matplotlib inline into the code, the anaconda ipython works correctly now. But if I replace the %matplotlib inline with %pylab, the anaconda ipython died again.
Note: I use the python 2.7. System ipython's version is 2.3, anaconda ipython's version is 3.2.

To fix this problem, I added to the top:
import matplotlib
matplotlib.use('Agg')
I have a CentOS7, Anaconda3-4.5.0, python3.5
pyqt version 5.6.0 and qt version 5.6.0
If you want to use the plot in a Tkinter window for visualisation then use:
matplotlib.use('TkAgg')

Same problem with Linux Mint 17, 64 bit. It was solved after 4h searching on the net! You need to give these commands on the terminal from folder /anaconda2/bin
sudo ./conda remove qt
sudo ./conda remove pyqt
sudo ./conda install qt
sudo ./conda install pyqt
Hope it helps!

I experienced this problem on Ubuntu 16.04 with anaconda 4.3.17 (Python 2.7). The issue stemmed from anaconda having Qt version 5.6 installed, while my system Qt libraries were at version 5.5.
A quick hack is to make sure Anaconda libraries precede your system libraries by setting LD_LIBRARY_PATH:
export LD_LIBRARY_PATH=$HOME/anaconda2/lib:$LD_LIBRARY_PATH
Unfortunately, this will break other programs that use Qt 5.5, so you can only use it in situations that are 100% anaconda python, for example, if you're starting an ipython session with --pylab.
I discovered that this was the issue by looking at the way libxqcb.so was linked:
ldd $HOME/anaconda2/plugins/platforms/libqxcb.so
which reported the following errors:
./libqxcb.so: /usr/lib/x86_64-linux-gnu/libQt5XcbQpa.so.5: version `Qt_5_PRIVATE_API' not found (required by ./libqxcb.so)
./libqxcb.so: /usr/lib/x86_64-linux-gnu/libQt5Gui.so.5: version `Qt_5' not found (required by ./libqxcb.so)
./libqxcb.so: /usr/lib/x86_64-linux-gnu/libQt5Core.so.5: version `Qt_5' not found (required by ./libqxcb.so)
./libqxcb.so: /usr/lib/x86_64-linux-gnu/libQt5Core.so.5: version `Qt_5.6' not found (required by ./libqxcb.so)
The error messages are saying they can't find Qt_5.6, which is anaconda's version. The version on my system was 5.5, which I found out by looking at the filenames that resulted from this command:
ls /usr/lib/x86_64-linux-gnu/libQt*

This can also happen when using something like an ssh session to connect to for instance a piece of software that is using X11 graphical libraries as I recently discovered.
The issue occurs when trying to plot images in the graphical user interface, using:
cv2.imshow()
or
plot.show()
In this case you should either use ssh -XY or you should consider saving to a file instead such as
cv2.imwrite('file.png')
or
plt.savefig('file.png')
I had this problem when using a deep neural network that was originally geared towards being used in conjunction with Jupyter notebook. When writing the images to files instead of displays, the code the ran through multiple ssh tunnels without reporting the xcb and Qt bug.
By the way, it didn't matter what variant of Qt4 or Qt5 was installed. It still failed with the same error message.

I had to upgrade several python packages to make it work. In particular:
pip3 install --upgrade pyside2 pyqt5

Related

python vtk 8.2.0 seems to have broken attributes

I am working with a group of people using Ubuntu 16.04 with Python 3.7.3 running Anaconda 2019.03.
For a new project I was trying to use VTK and the version installed was 8.2.0. Running the example from here:
https://lorensen.github.io/VTKExamples/site/Python/IO/ReadSTL/
produces an error "module 'vtk' has no attribute 'vtkSTLReader'".
In further investigation the vtk module only has 9 functions and attributes missing all the ones I am used to.
I moved to another machine where I was easily able to run the example. That machines was ubuntu 20.04 with Anaconda installed with python 3.65 and VTK 8.1.1. I updated anaconda with
conda update conda
conda update --all
This resulted in vtk getting updated to VTK 8.2.0 however now if I simply run a simple python code
import vtk
vtk.
The same thing happened as the work computer. The auto complete after typing period no longer gives a list of all the functions and attributes. And trying to run old code that worked gives errors that the functions and attributes are not available in vtk. I uninstalled vtk from conda and installedthe latest VTK version 9.0.1 using pip and the error is the same. Are there dependencies that don't get updated by conda when you update vtk?
Other things that I tested still work so it just seems like VTK broke. I looked through the API changes between vtk8.1.1 and 8.2.0 and although there are a few it doesn't change some of the ones that are missing like vtkRenderer() or vtkSTLReader().
I was able to go back to vtk8.1.1 and verify it works but I would really like to use vtk 8.2 or greater because there are a group of people using python 3.7 and it would be impractical to have them all move back to Python 3.65 for this.
I am basing the need for VTK >8.2 based on a post I read where python3.7 needs vtk >8.2.
Based on this article I also tried installing python-vtk7 but the package was not found for ubuntu 16.04.
Setting up VTK, with python3.8 and Ubuntu 20.04
I assume I have a mismatch with something simple but just not sure what. I would appreciate if anyone has any insight into if I need to update something else in addition to the conda update.
Adding a little more detail:
I tried pip install python-vtk as well. This installs python-vtk 0.1.0 and replaces vtk with version 8.1.2 with the same error messages. Moving back to python 3.65 and vtk 8.1.1 fixes the problem but I haven't been able to get any version to work with Python 3.7.
Update:
I asked for information through VTK as well.
https://discourse.vtk.org/t/installing-vtk-8-2-for-python-3-7-3-no-attributes-available/3821/4
I add a debug command in the vtk.py file. If I call the import vtk from a editor in spyder the debug does not get printed, but if I call the import vtk from the console in spyder it does. I have no idea why those two would be different.
Thanks
The issue ended up being my openGL support. I had three machines. One was running VMWARE which showed the issue. One was running VNC which showed the issue. And one was running ubuntu natively (using the GPU HW ) which worked fine.
I was running with VMWARE and that worked OK with python 2.x and earlier versions of VTK but when I moved to Python 3x and later versions of VTK it broke because it didn't support the right version of OpenGL. It looks like there are couple different ways to fix it, but in my case the one I used was to use a SW implementation of OpenGL to guarantee support. This was the fastest solution. The other solution would be with a virtual GL driver which leverages the HW and is likely faster and I will likely look at in the future.
For the SW implementation of OpenGL:
sudo apt install llvm-dev # make sure SW driver that supports OPENGL3.2
export LIBGL_ALWAYS_SOFTWARE=1 # Ensures vtk uses sw opengl driver: this needs to be called from each new terminal or alternatively make persistent in bashrc

"UserWarning: Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure." when plotting figure with pyplot on Pycharm

I am trying to plot a simple graph using pyplot, e.g.:
import matplotlib.pyplot as plt
plt.plot([1,2,3],[5,7,4])
plt.show()
but the figure does not appear and I get the following message:
UserWarning: Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure.
I saw in several places that one had to change the configuration of matplotlib using the following:
import matplotlib
matplotlib.use('TkAgg')
import matplotlib.pyplot as plt
I did this, but then got an error message because it cannot find a module:
ModuleNotFoundError: No module named 'tkinter'
Then, I tried to install "tkinter" using pip install tkinter (inside the virtual environment), but it does not find it:
Collecting tkinter
Could not find a version that satisfies the requirement tkinter (from versions: )
No matching distribution found for tkinter
I should also mention that I am running all this on Pycharm Community Edition IDE using a virtual environment, and that my operating system is Linux/Ubuntu 18.04.
I would like to know how I can solve this problem in order to be able to display the graph.
Solution 1: is to install the GUI backend tk
I found a solution to my problem (thanks to the help of ImportanceOfBeingErnest).
All I had to do was to install tkinter through the Linux bash terminal using the following command:
sudo apt-get install python3-tk
instead of installing it with pip or directly in the virtual environment in Pycharm.
Solution 2: install any of the matplotlib supported GUI backends
solution 1 works fine because you get a GUI backend... in this case the TkAgg
however you can also fix the issue by installing any of the matplolib GUI backends like Qt5Agg, GTKAgg, Qt4Agg, etc
for example pip install pyqt5 will fix the issue also
NOTE:
usually this error appears when you pip install matplotlib and you are trying to display a plot in a GUI window and you do not have a python module for GUI display.
The authors of matplotlib made the pypi software deps not depend on any GUI backend because some people need matplotlib without any GUI backend.
In my case, the error message was implying that I was working in a headless console. So plt.show() could not work. What worked was calling plt.savefig:
import matplotlib.pyplot as plt
plt.plot([1, 2, 3], [5, 7, 4])
plt.savefig("mygraph.png")
I found the answer on a github repository.
If you use Arch Linux (distributions like Manjaro or Antegros) simply type:
sudo pacman -S tk
And all will work perfectly!
Simple install
pip3 install PyQt5==5.9.2
It works for me.
Try import tkinter because pycharm already installed tkinter for you, I looked Install tkinter for Python
You can maybe try:
import tkinter
import matplotlib
import matplotlib.pyplot as plt
matplotlib.use('TkAgg')
plt.plot([1,2,3],[5,7,4])
plt.show()
as a tkinter-installing way
I've tried your way, it seems no error to run at my computer, it successfully shows the figure. maybe because pycharm have tkinter as a system package, so u don't need to install it. But if u can't find tkinter inside, you can go to Tkdocs to see the way of installing tkinter, as it mentions, tkinter is a core package for python.
I added %matplotlib inline
and my plot showed up in Jupyter Notebook.
The answer has been given a few times but it is not obvious, one needs to install graphics, this works.
pip3 install PyQt5
I too had this issue in PyCharm. This issue is because you don't have tkinter module in your machine.
To install follow the steps given below (select your appropriate os)
For ubuntu users
sudo apt-get install python-tk
or
sudo apt-get install python3-tk
For Centos users
sudo yum install python-tkinter
or
sudo yum install python3-tkinter
for Arch Users
sudo pacman -S tk
or
sudo pamac install tk
For Windows, use pip to install tk
After installing tkinter restart your Pycharm and run your code, it will work
This worked with R reticulate. Found it here.
1: matplotlib.use( 'tkagg' )
or
2: matplotlib$use( 'tkagg' )
For example:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import style
import matplotlib
matplotlib.use( 'tkagg' )
style.use("ggplot")
from sklearn import svm
x = [1, 5, 1.5, 8, 1, 9]
y = [2, 8, 1.8, 8, 0.6, 11]
plt.scatter(x,y)
plt.show()
If using Jupyter notebook try the following:
%matplotlib inline
This should render the plot even if not specifying the
plt.show()
command.
None of these answers worked for me using Pycharm Professional edition 2021.3
Regular matplotlib graphs did work on the scientific view, but it did not allow me to add images to the plots.
What did work for me is adding this line before I try plotting anything:
plt.switch_backend('TkAgg')
issue = “UserWarning: Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure.”
And this worked for me
import matplotlib
import matplotlib.pyplot as plt
matplotlib.use('Qt5Agg')
For Windows 10, if using pip install tk does not work for you, try:
Download and run official python installer for windows. Even if you
already have it downloaded, run it again.
When (re)installing python, make sure you chose "advanced" options, and
set the checkbox "tcl/tk and IDLE" to true.
If you already had python installed, select the "Modify" option, and
make sure that checkbox is selected.
Source of my fix:
https://stackoverflow.com/a/59970646/2506354
I have solved it by putting matplotlib.use('TkAgg') after all import statements.
I use python 3.8.5 VSCODE and anaconda.
No other tricks worked.
I installed python3-tk , on Ubuntu 20.04 and using WSL2
import numpy as np
import matplotlib.pyplot as plt
import matplotlib
matplotlib.use( 'tkagg')
and then I installed GWSL from the Windows Store which seems to solve problem of WSL2 rendering out of the box
This will solve the issue. It works well in jupyter.
%matplotlib inline
The comment by #xicocaio should be highlighted.
tkinter is python version-specific in the sense that sudo apt-get install python3-tk will install tkinter exclusively for your default version of python. Suppose you have different python versions within various virtual environments, you will have to install tkinter for the desired python version used in that virtual environment. For example, sudo apt-get install python3.7-tk. Not doing this will still lead to No module named ' tkinter' errors, even after installing it for the global python version.
On Mac OS, I made it work with:
import matplotlib
matplotlib.use('MacOSX')
Ubuntu 20.04 command line setup. I install the following to make Matplotlib stop throwing the error UserWarning: Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure.
I installed python-tk through the steps:
apt-get update
apt-get install python3.8-tk
Just in case if this helps anybody.
Python version: 3.7.7
platform: Ubuntu 18.04.4 LTS
This came with default python version 3.6.9, however I had installed my own 3.7.7 version python on it (installed building it from source)
tkinter was not working even when the help('module') shows tkinter in the list.
The following steps worked for me:
sudo apt-get install tk-dev.
rebuild the python:
1. Navigate to your python folder and run the checks:
cd Python-3.7.7
sudo ./configure --enable-optimizations
Build using make command:
sudo make -j 8 --- here 8 are the number of processors, check yours using nproc command.
Installing using:
sudo make altinstall
Don't use sudo make install, it will overwrite default 3.6.9 version, which might be messy later.
Check tkinter now
python3.7 -m tkinter
A windows box will pop up, your tkinter is ready now.
After upgrading lots of packages (Spyder 3 to 4, Keras and Tensorflow and lots of their dependencies), I had the same problem today! I cannot figure out what happened; but the (conda-based) virtual environment that kept using Spyder 3 did not have the problem. Although installing tkinter or changing the backend, via matplotlib.use('TkAgg) as shown above, or this nice post on how to change the backend, might well resolve the problem, I don't see these as rigid solutions. For me, uninstalling matplotlib and reinstalling it was magic and the problem was solved.
pip uninstall matplotlib
... then, install
pip install matplotlib
From all the above, this could be a package management problem, and BTW, I use both conda and pip, whenever feasible.
You can change the matplotlib using backend using the from agg to Tkinter TKAgg using command
matplotlib.use('TKAgg',warn=False, force=True)
Works if you use some third party code in your project. It probably contains the following line
matplotlib.use('Agg')
Search for it and comment it out.
If you have no clue about what it is you are probably not using this part of the code.
Solutions about using another backend GUI may be cleaner, so choose your fighter.
The solution that worked for me:
Install tkinter
import tkinter into the module
make sure that matplotlib uses (TkAgg) instead of (Agg)
matplotlib.use('TkAgg')
execute the following command before plotting
%matplotlib inline
Try:
%matplotlib inline
I had the same problem and it worked for me. I tested it on my Jupyter notebooks and visual studio code, so you should have no problems.
On WSL with X server
Make sure that your X server work. Matplotlib indicate this error if he can't connect to the X display.
Windows Firewall configuration
Pay attention to the windows firewall ! I changed from WSL Debian to Ubuntu and didn't remember about the firewall rule.
I use this post to configure the windows firewall rule to make the X server work. This method avoid too permisive rule that able anyone to use your X server.
It said :
If you already had installed an X11 server, Windows may have created firewall rules that will mess with the above configuration. Search for them and delete them in "Windows Defender Firewall with Advanced Security."
You will now need to configure Windows Firewall to permit connections from WSL2 to the X11 display server. You will install the display server in the next step. We do this step first to avoid Windows Firewall from auto-creating an insecure firewall rule when you run the X11 display server. Many guides on X11 forwarding and WSL2 make this firewall rule too permissive, allowing connections from any computer to your computer. This means someone could theoretically, if they are on your same network, start sending graphical display information to your computer.
To avoid this, we will make Windows Firewall only accept internet traffic from the WSL2 instance.
To set this up, you can copy the below to a script and run it from within WSL2:
#!/bin/sh
LINUX_IP=$(ip addr | awk '/inet / && !/127.0.0.1/ {split($2,a,"/"); print a[1]}')
WINDOWS_IP=$(ip route | awk '/^default/ {print $3}')
# Elevate to administrator status then run netsh to add firewall rule
powershell.exe -Command "Start-Process netsh.exe -ArgumentList \"advfirewall firewall add rule name=X11-Forwarding dir=in action=allow program=%ProgramFiles%\VcXsrv\vcxsrv.exe localip=$WINDOWS_IP remoteip=$LINUX_IP localport=6000 protocol=tcp\" -Verb RunAs"
Manual method :
Alternatively, you can manually add the rule through a GUI by doing the following:
Open "Windows Defender Firewall with Advanced Security"
Click add new rule brings up the New Rule Wizard (next to navigate between each section):
Rule type: Custom
Program: "This program path:" %ProgramFiles%\VcXsrv\vcxsrv.exe
Protocol and ports
Protocol type: TCP
Local port: 6000
Remote port: any
Scope
Local IP address: Obtain the IP address to put in by running the below command in WSL2
ip route | awk '/^default/ {print $3}'
remote IP addresses
Obtain IP address to enter by running the below in WSL2
ip addr | awk '/inet / && !/127.0.0.1/ {split($2,a,"/"); print a[1]}'
Action: "Allow the connection
Profile: Selection Domain, Private, and Public
Name: "X11 forwarding"
Linux Mint 19. Helped for me:
sudo apt install tk-dev
P.S. Recompile python interpreter after package install.
When I ran into this error on Spyder, I changed from running my code line by line to highlighting my block of plotting code and running that all at once. Voila, the image appeared.
If you install python versions using pyenv on Debian-based systems, be sure to run sudo apt install tk-dev before pyenv install. If it's already installed, remove it with pyenv uninstall and install it again after install tk-dev. Therefore, there is no need to set any env variables when running pyenv install.

Python spyder could not initialize GLX

Trying to setup a desktop station for image processing. I recently installed ubuntu 16.04 and went through the guide by Adrian to install python3, cv2, and virtual enviroments.
Instead of using cv2 3.1.0, I used cv2 3.2.0 but besides that, everything was the same.
after installing correctly and running the various tests, I confirmed that python3 was working and cv2 was linked to python.
As I am used to graphical interfaces, I decided to install spyder as it has a nice GUI system. I have done this before on 2 separate devices (a laptop and a raspberry pi 3). However, upon installing on this desktop computer I ran into a weird problem.
When ever I run spyder, It results in the error below
Could not initialize GLX
Aborted (core dumped)
I have been looking online for solutions but I cannot seem to find an answer to this problem
edit: I went searching for core dump files on ubuntu and I found a bunch of files all containing the same code 00000033.
I am running Ubuntu on a Raspberry Pi and was having the same issue with Spyder.
My original issue looked like this when trying to run spyder:
qt.glx: qglx_findConfig: Failed to finding matching FBConfig for QSurfaceFormat(version 2.0, options QFlags<QSurfaceFormat::FormatOption>(), depthBufferSize -1, redBufferSize 1, greenBufferSize 1, blueBufferSize 1, alphaBufferSize -1, stencilBufferSize -1, samples -1, swapBehavior QSurfaceFormat::SingleBuffer, swapInterval 1, colorSpace QSurfaceFormat::DefaultColorSpace, profile QSurfaceFormat::NoProfile)
Could not initialize GLX
Aborted (core dumped)
I tried most everything on this thread and was finally able to get it to work by exporting the following variable under the user I was trying to run it as:
export QT_XCB_GL_INTEGRATION=none
This was found in a QuteBrowser github issue thread covering a similar problem:
https://github.com/NixOS/nixpkgs/issues/66755#issuecomment-657305962
First of all, for our understanding:
GLX (initialism for "OpenGL Extension to the X Window System") is an
extension to the X Window System core protocol providing an interface
between OpenGL and the X Window System as well as extensions to OpenGL
itself. It enables programs wishing to use OpenGL to do so within a
window provided by the X Window System. GLX distinguishes two
"states": indirect state and direct state.
Any way, I had a similar problem with QT installation and solved it installing the following package: libgl1-mesa-dri
sudo apt-get install libgl1-mesa-dri
Another similar solution I found was:
sudo apt-get purge nvidia*
sudo cp /etc/X11/xorg.conf /etc/X11/xorg.conf.orig
sudo rm /etc/X11/xorg.conf
For 32-bit,
sudo apt-get install --reinstall xserver-xorg-core libgl1-mesa-glx:i386 libgl1-mesa-dri:i386
For 64-bit
sudo apt-get install --reinstall xserver-xorg-core libgl1-mesa-glx:amd64 libgl1-mesa-dri:amd64
sudo dpkg-reconfigure xserver-xorg
I hope it helps you.
I had the same issue, using x2go. After trying everything mentioned above, the only solution I found was to launch spyder in a different way. Before this, I would so as follows (and it worked perfectly for spyder 4):
source activate [name of your virtual environment]
spyder&
Since last week, the 'spyder&' command no longer launches the IDE, and I managed to surpass this by writing instead:
source activate [name of your virtual environment]
spyder3
The terminal still complains about certain issues, but at least I can work now!
I had this problem, spyder was not starting when using a ssh connection and I had the error :
Could not initialize GLX.
This was because the qt version was to recent : 5.9
I could not solve it by installing pyopengl, nor any other fixes that I found on the web.
Simply installing qt 5.6.2 (with conda -c conda-forge) did not work either because it downgraded my version of matplotlib to 2.2
I got it working by reinstalling a full conda environment to make sure I have the matplotlib version that I wanted : 3.1.1
Solution :
conda install -c conda-forge matplotlib=3.1 qt=5.6.2 spyder

PyQt problems with Spyder on Ubuntu 14.04 LTS

I've installed the latest version of Anaconda. The install went smoothly, but when I try to run Spyder I get a couple of run-time errors relating to PySide and PyQt. The final error message is:
RuntimeError: Please check Spyder installation requirements:
PyQt4 4.6+ (or PySide 1.2.0+) is required.
However, when I try to install PyQt, Ubuntu says I have the most up to date version. Also, conda list indicates that I have the following installed (I've excluded the other stuff as it isn't relevant):
pyqt 4.11.4 py35_1
qt 4.8.7 1
So Ubuntu seems to recognise that I have PyQt installed, but Spyder can't 'see' it.
To fix this, I attempted to install spyder3 via the package manager using
sudo apt-get install spyder3
The install completed without any errors, but running spyder3 led to the same PyQt error as before.
Could someone please advise me how to fix this? I'm fairly new to Ubuntu, so am not sure how to link Spyder to the required Qt files.
Finally managed to fix this and figured it was worth posting an update in case anyone else has the same issue.
After installing Anaconda I installed some 3rd party software (moose framework) which requires a series of modules to be loaded. One of these is miniconda.
The presence of miniconda seems to confuse Spyder - and stops it from working. By using:
module unload miniconda
I found that Spyder works as normal.

Running Anaconda Python Examples

I'd like to run any example .py script that uses matplotlib or pyqtgraph to see how it looks.
The problem is, none of the examples included with the Anaconda3-2.0.1-Windows-x86 install work. For example, I open Spyder3.4 that was installed with Anaconda, but when I open/run any of the .py files ( C:\Anaconda3\Lib\site-packages\matplotlib) nothing happens... No error message, nothing in the output tab.
I've installed this on a WinXp virtual machine that also has Qt5 creator/designer and PyQt installed. Is there any known conflict or path issues having PyQt installed alongside Anaconda?
How can I tell if Anaconda and its packages (namely pyqtgraph, matplotlib and pyqt) installed correctly?
You don't run the files in site-packages directly to use the modules. You should start a Python console in Spyder or the IPython notebook and use import matplotlib to access matplotlib.

Categories

Resources