Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

X11 forwarding with PyCharm and Docker Interpreter

I am developing a project in PyCharm using a Docker interpreter, but I am running into issues when doing most "interactive" things. e.g.,

import matplotlib.pyplot as plt
plt.plot([1, 2, 3], [4, 5, 6])

gives

RuntimeError: Invalid DISPLAY variable

I can circumvent this using

import matplotlib
matplotlib.use('agg')

which gets rid of the error, but no plot is produced when I do plt.show(). I also get the same error as in the thread [pycharm remote python console]: "cannot connect to X server" error with import pandas when trying to debug after importing Pandas, but I cannot SSH into my docker container, so the solution proposed there doesn't work. I have seen the solution of passing "-e DISPLAY=$DISPLAY" into the "docker run" command, but I don't believe PyCharm has any functionality for specifying command-line parameters like this with a Docker interpreter. Is there any way to set up some kind of permanent, generic X11 forwarding (if that is indeed the root cause) so that the plots will be appropriately passed to the DISPLAY on my local machine? More generally, has anyone used matplotlib with a Docker interpreter in PyCharm successfully?

like image 566
Charles Davis Avatar asked Aug 25 '16 21:08

Charles Davis


2 Answers

Here's the solution I came up with. I hope this helps others. The steps are as follows:

  1. Install and run Socat

    socat TCP-LISTEN:6000,reuseaddr,fork UNIX-CLIENT:\"$DISPLAY\"

  2. Install and run XQuartz (probably already installed)

  3. Edit the PyCharm run/debug configuration for your project, setting the appropriate address for the DISPLAY variable (in my case 192.168.0.6:0)

Running/debugging the project results in a new quartz popup displaying the plotted graph, without any need to save to an image, etc.

like image 91
JP. Avatar answered Nov 12 '22 04:11

JP.


Run xhost + on the host and add these options to the docker run: -e DISPLAY -v /tmp/.X11-unix:/tmp/.X11-unix

like image 42
Ricardo Branco Avatar answered Nov 12 '22 05:11

Ricardo Branco