0

While I am trying to execute python code with wx package in SSH getting below error. "Unable to access the X Display, is $DISPLAY set properly?"

I need to catch this in exception block but this error is not coming under any built-in python exceptions type.

It would be helpful if anyone handled the same situation.

Nithya
  • 614
  • 2
  • 11
  • 28
  • 2
    always put full error message (starting at word "Traceback") in question (not comment) as text (not screenshot). There are other useful information. – furas Feb 17 '20 at 11:58
  • I am getting this error message alone in SSH command line – Nithya Feb 17 '20 at 12:00
  • if I understand you try to run program with GUI (wx) on server (ssh). But SSH doesn't run Windows/XWindows so it doesn't have variable $DISPLAY which programs with GUI needs to know on which monitor display window. And it means monitor connected to server, not to your local computer. – furas Feb 17 '20 at 12:06
  • @furas so he needs an X server for his local machine(something like xming for example)? could it be that X forwarding is not on in the ssh server? – Nullman Feb 17 '20 at 12:08
  • @Nullman I never did it but server would use `$DISPLAY` which directs all windows to local computer (forwarded/tunneled by ssh to local computer) and local computer should run X server to display it. – furas Feb 17 '20 at 12:12

1 Answers1

2

Based on the answer in this post, you can get this behaviour in a python script as follows:

import subprocess
output = subprocess.check_output(["xset", "q"], stderr=subprocess.STDOUT)
print(output)

This script will printout the results of the xset q command if an X server is available. If I damage my $DISPLAY variable then it fails with a python exception: subprocess.CalledProcessError: Command '['xset', 'q']' returned non-zero exit status 1

So you could put this in a try: and if it fails, you know something is up with displaying the X window. It might be due to a bad or missing DISPLAY variable, or something else but it should indicate that attempting to show the wx window is likely to fail.

RFairey
  • 736
  • 3
  • 9
  • I dont want to fix the error.I just want to catch the error with specified exception.But not sure about which type of exception this error will fall under like Systemerror or oserror or etc – Nithya Feb 17 '20 at 13:40
  • It's a `subprocess.CalledProcessError`, which you can catch just the same as other exceptions. – RFairey Feb 17 '20 at 13:47
  • I agree that.But I am not running code with subprocess.I am getting this error while trying import one module which uses 'wx' – Nithya Feb 17 '20 at 13:56
  • OK. My suggestion is to give you a way of detecting if an X server can be contacted, but you are asking for a way to cause `import wx` to throw an exception if it would lead to the above message? – RFairey Feb 17 '20 at 14:00
  • exactly.Its like I knew the error but no idea about what type of error it is to catch that. – Nithya Feb 17 '20 at 14:09