33

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.

Gengiolo
  • 603
  • 1
  • 6
  • 14
  • 2
    I found out that pycharm does not use the system ssh. Instead it uses [JSch](http://www.jcraft.com/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 Nov 01 '17 at 22:53
  • 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 Nov 02 '17 at 20:30
  • They have an open issue about this here: https://youtrack.jetbrains.com/issue/PY-13869 Looks like there is no official solution at the moment :( – MZHm Nov 06 '17 at 18:49
  • Maybe you should try external ssh tool as described here https://www.jetbrains.com/help/pycharm/using-the-pycharm-built-in-ssh-terminal-and-remote-ssh-external-tools.html. You can specify additional ssh parameters there – Alexey Barsuk Nov 06 '17 at 19:18
  • @AlexeyBarsuk Nope, sadly that's for a program which can be run **over** the established ssh connection... – Gengiolo Nov 20 '17 at 17:02
  • 1
    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 Apr 08 '21 at 20:56

5 Answers5

7

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

Treed
  • 3
  • 2
Tarun Lalwani
  • 142,312
  • 9
  • 204
  • 265
  • Hi Tarun! Thank you for your solution. Now the variable $DISPLAY seems to be automatically set but if I run a command like "eog img.png" I get: "** (eog:40789): WARNING **: Could not open X display". Do you know how to fix this? – Gengiolo Nov 06 '17 at 23:45
  • @Gengiolo, I tested and it probably needs calling of few more methods http://www.jcraft.com/jsch/examples/X11Forwarding.java.html. Unfortunately I don't have time for debugging further on this issue. But this should give you a direction to explore and how it needs to be done – Tarun Lalwani Nov 07 '17 at 09:06
  • thank you for your effort, you deserve the bounty! :) I'm gonna look into it and update this thread ASAP! – Gengiolo Nov 07 '17 at 16:51
  • This didn't work for me. What version of Pycharm did you use? Thanks – aarbelle Apr 15 '20 at 17:50
  • 1
    You can see in screenshot it was `2017.2.4` something – Tarun Lalwani Apr 15 '20 at 18:10
6

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

oezguensi
  • 930
  • 1
  • 12
  • 23
  • So you still need to make an external ssh connection outside Pycharm? – Gengiolo May 07 '20 at 11:38
  • No I didn't need to. How would that even affect running code in PyCharm? – oezguensi May 07 '20 at 11:51
  • It's explained in the question description as well as in other answers: PyCharm does not use the system ssh and the connection is opened by default without X11 forwarding. So you need to open another external ssh connection to make it work. How did you test your solution? A simple test is to show an image with matplolib, if the matplolib GUI appears on screen it means it works. Let us know if you can do that! – Gengiolo May 07 '20 at 12:19
  • I added a "proof" – oezguensi May 07 '20 at 15:34
  • 3
    The PyCharm "SciView" is not using X11 forwarding: https://stackoverflow.com/questions/48334853/using-pycharm-i-want-to-show-plot-extra-figure-windows it may be useful if you want to just see images but it cannot be used for other X windows applications! – Gengiolo May 07 '20 at 16:54
  • 1
    Oh I understand. I didn't know that. Thank you for the clarification. I was struggling to plot images in SciView and this approach worked for me. – oezguensi May 07 '20 at 17:00
  • Unfortunately didn't work for me on Ubuntu but seems really promising – vozman May 22 '20 at 15:33
  • 1
    I got this working in Linux Mint, but I have to add a user environment variable DISPLAY and set it to :10.0 – docPhil Jun 25 '20 at 10:07
  • @Gengiolo how does that link clarify that SciView doesn't use X11 forwarding? – Homero Esmeraldo Apr 08 '21 at 20:45
  • @oezguensi how did this even work if it is really true that SciView doesn't use X11 forwarding? Were there any other configurations you used? Thank you – Homero Esmeraldo Apr 08 '21 at 20:59
  • @HomeroEsmeraldo it may have been just the 3rd point which solved the issue – oezguensi Apr 09 '21 at 08:01
  • Nevermind, I was missing the `plt.show`. I'm on Windows, which I believe doesn't set the DISPLAY variable, so it works even without that 3rd point. `plt.show` seems mysterious, sometimes it works without it and sometimes it doesn't. Anyway, thank you! – Homero Esmeraldo Apr 10 '21 at 06:29
  • Hm... maybe it needs the `plt.show` when it is plotting at PyCharm visualizer, but it doesn't when it is on the system window X... – Homero Esmeraldo Apr 10 '21 at 06:32
2

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.

Vladimir Lagunov
  • 1,895
  • 15
  • 15
0

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)

Hesham Eraqi
  • 2,444
  • 4
  • 23
  • 45
  • Hi Hesham, thanks for the suggestion but I'd prefer not to open anything in parallel. In the end it's not a big deal to open an external terminal or even update manually the DISPLAY variable, but it would be nice to have everything running within PyCharm, it is just crazy to think that you can't! – Gengiolo Feb 06 '19 at 09:43
0

Update 2022: for PyCharm newer than 2022.1: Plotting in SciView works by only setting ForwardX11 yes in .ssh/config (my laptop OS is ubuntu 22.04). I did not set any other parameters either on the server or local side.

Yiwei Jiang
  • 126
  • 8