30
votes

The Question

I'm trying to enable X11 forwarding through the PyCharm SSH Terminal which can be executed via

"Tools -> Start SSH session..."

Unfortunately, It seems there is no way of specifying the flags like I would do in my shell for enabling the X11 Forwarding:

ssh -X user@remotehost

Do you know some clever way of achieving this?


Current dirty solution

The only dirty hack I found is to open an external ssh connection with X11 forwarding and than manually update the environment variable DISPLAY.

For example I can run on my external ssh session:

vincenzo@remotehost:$ echo $DISPLAY
localhost:10.0

And than set on my PyCharm terminal:

export DISPLAY=localhost:10.0

or update the DISPLAY variable in the Run/Debug Configuration, if I want to run the program from the GUI.

However, I really don't like this solution of using an external ssh terminal and manually update the DISPLAY variable and I'm sure there's a better way of achieving this!

Any help would be much appreciated.


P.s. Making an alias like:

alias ssh='ssh -X'

in my .bashrc doesn't force PyCharm to enable X11 forwarding.

4
I found out that pycharm does not use the system ssh. Instead it uses JSch which is a pure java implementation of ssh. JSch supports x11 forwarding, it is not enabled by default though.One way to solve your problem would be to replace jsch.jar that is bundled with pycharm with a custom version with different defaults.Oliver Weissbarth
Thank you @OliverWeissbarth for your comment! If you'd be so kind to provide a bit more details and a step-by-step solution in an actual answer I'd be very happy to mark it as approved!Gengiolo
They have an open issue about this here: youtrack.jetbrains.com/issue/PY-13869 Looks like there is no official solution at the moment :(MZHm
Maybe you should try external ssh tool as described here jetbrains.com/help/pycharm/…. You can specify additional ssh parameters thereAlexey Barsuk
Actually, I really appreciate your solution already. Thank you for that. I initially had a problem reproducing it because I was setting matplotlib backed with matplotlib.use('Agg'), but when I commented that line it worked like a charm. Just leaving this comment as a heads up for anyone else with the same issue.Homero Esmeraldo

4 Answers

7
votes

So I was able to patch up jsch and test this out and it worked great.

Using X11 forwarding

You will need to do the following to use X11 forwarding in PyCharm:
- Install an X Server if you don't already have one. On Windows this might be the VcXsrv project, on Mac OS X the XQuartz project.
- Download or compile the jsch package. See instructions for compilation below.
- Backup jsch-0.1.54.jar in your pycharm's lib folder and replace it with the patched version. Start Pycharm with a remote environment and make sure to remove any instances of the DISPLAY environment variable you might have set in the run/debug configuration.

Compilation

Here is what you need to do on a Mac OS or Linux system with Maven installed.

wget http://sourceforge.net/projects/jsch/files/jsch/0.1.54/jsch-0.1.54.zip/download
unzip download
cd jsch-0.1.54
sed -e 's|x11_forwarding=false|x11_forwarding=true|g' -e 's|xforwading=false|xforwading=true|g' -i src/main/java/com/jcraft/jsch/*.java
sed -e 's|<version>0.1.53</version>|<version>0.1.54</version>|g' -i pom.xml
mvn clean package

This will create jsch-0.1.54.jar in target folder.

X11 Enabled

5
votes

Update 2020: I found a very easy solution. It may be due to the updated PyCharm version (2020.1).

  1. Ensure that X11Forwarding is enabled on server: In /etc/ssh/sshd_config set
X11Forwarding yes
X11DisplayOffset 10
X11UseLocalhost no
  1. On client (MacOS for me): In ~/.ssh/config set
ForwardX11 yes
  1. In PyCharm deselect Include system environment variables. This resolves the issue that the DISPLAY variable gets set to the system variable. enter image description here

EDIT: As seen in the below image it works. For example I used the PyTorch implementation of DeepLab and visualize sample images from PASCAL VOC: enter image description here

1
votes

X11 forwarding was implemented in 2021.1 for all IntelliJ-based IDEs. If it still doesn't work, please consider creating a new issue at youtrack.jetbrains.com.

By the way, the piece of advice about patching jsch won't work for any IDE newer than 2019.1.

0
votes

In parallel, open MobaXTerm and connect while X11 forwarding checkbox is enabled. Now PyCharm will forward the display through MobaXTerm X11 server. This until PyCharm add this 'simple' feature.

Also, set DISPLAY environment variable in PyCharm run configuration like this: DISPLAY=localhost:10.0 (the right hand side should be obtained with the command echo $DISPLAY in the server side)