3

I am using mss library in Python to take a screenshot and save it on the current path. It is working perfectly on my Mac using PyCharm. But when I try to run the same thing on Ubuntu 16 I get an error, even when I am doing exactly as the Docs says:

>>> from mss import mss
>>> with mss() as sct:
...     sct.shot()

but I am getting this error, how to fix this ?

Traceback (most recent call last):
  File "/usr/local/lib/python3.5/dist-packages/mss/linux.py", line 132, in __init__
    display = os.environ['DISPLAY'].encode('utf-8')
  File "/usr/lib/python3.5/os.py", line 725, in __getitem__
    raise KeyError(key) from None
KeyError: 'DISPLAY'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.5/dist-packages/mss/factory.py", line 34, in mss
    return MSS(**kwargs)
  File "/usr/local/lib/python3.5/dist-packages/mss/linux.py", line 134, in __init__
    raise ScreenShotError('$DISPLAY not set.', locals())
mss.exception.ScreenShotError: ('$DISPLAY not set.', {'display': None, 'self': <mss.linux.MSS object at 0x7f06ce881d30>})
Tiger-222
  • 6,677
  • 3
  • 47
  • 60
Lucas123
  • 61
  • 1
  • 1
  • 5
  • Is `DISPLAY` in fact set? – kindall Mar 15 '18 at 20:10
  • I am not sure what is that. Could you please give a hint so that I can read about it or try to check if it is or not – Lucas123 Mar 15 '18 at 20:13
  • There are plenty of good Google results for `DISPLAY variable`. Here's one. https://askubuntu.com/questions/432255/what-is-the-display-environment-variable – kindall Mar 15 '18 at 20:35

1 Answers1

2

Indeed, on GNU/Linux you must have the DISPLAY envar set. If not, it is not common and you could try setting the display keyword argument like:

>>> with mss(display=':0') as sct:
...     sct.shot()
Tiger-222
  • 6,677
  • 3
  • 47
  • 60
  • Little reminder: [stackoverflow.com/help/someone-answers](https://stackoverflow.com/help/someone-answers), if the answer is good for you :) – Tiger-222 Jan 26 '19 at 16:25