2

I have a problem that just drives me crazy. I have a python script, that works when I run it in the terminal (ubuntu) but not in vscode.

The terminal code is just:

python helper.py

and the helper.py looks as follows:

from pynput.keyboard import Key, Controller
from pynput.keyboard import Listener

def on_press(key):
    print('{0} pressed'.format(
        key))

def on_release(key):
    print('{0} release'.format(
        key))
    if key == Key.esc:
        # Stop listener
        return False

# Collect events until released
with Listener(
        on_press=on_press,
        on_release=on_release) as listener:
    listener.join()

When I run it in the terminal it works fine, but in vscode it says

Traceback (most recent call last):
  File "/home/paul/Programming/statFit2/helper.py", line 1, in <module>
    from pynput.keyboard import Key, Controller
  File "/home/paul/anaconda3/lib/python3.7/site-packages/pynput/__init__.py", line 40, in <module>
    from . import keyboard
  File "/home/paul/anaconda3/lib/python3.7/site-packages/pynput/keyboard/__init__.py", line 52, in <module>
    from ._xorg import KeyCode, Key, Controller, Listener
  File "/home/paul/anaconda3/lib/python3.7/site-packages/pynput/keyboard/_xorg.py", line 39, in <module>
    from pynput._util.xorg import (
  File "/home/paul/anaconda3/lib/python3.7/site-packages/pynput/_util/xorg.py", line 40, in <module>
    _check()
  File "/home/paul/anaconda3/lib/python3.7/site-packages/pynput/_util/xorg.py", line 38, in _check
    display = Xlib.display.Display()
  File "/home/paul/anaconda3/lib/python3.7/site-packages/Xlib/display.py", line 89, in __init__
    self.display = _BaseDisplay(display)
  File "/home/paul/anaconda3/lib/python3.7/site-packages/Xlib/display.py", line 71, in __init__
    protocol_display.Display.__init__(self, *args, **keys)
  File "/home/paul/anaconda3/lib/python3.7/site-packages/Xlib/protocol/display.py", line 84, in __init__
    name, protocol, host, displayno, screenno = connect.get_display(display)
  File "/home/paul/anaconda3/lib/python3.7/site-packages/Xlib/support/connect.py", line 73, in get_display
    return mod.get_display(display)
  File "/home/paul/anaconda3/lib/python3.7/site-packages/Xlib/support/unix_connect.py", line 76, in get_display
    raise error.DisplayNameError(display)
Xlib.error.DisplayNameError: Bad display name ""

Its just one of those things that really don't make sense to me and which really want to make me just quit programming.

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Paul Schal
  • 21
  • 1
  • 2
  • 1
    Is the default interpreter for VS Code different than the default Python interpreter for you terminal? – Chrispresso Apr 20 '20 at 15:58
  • how can i see this? i think it is the same anaconda version – Paul Schal Apr 20 '20 at 16:02
  • In VS code, hit `ctrl + shift + p` and type `python: select interpreter` and see which is the default. For Ubuntu type `which python`. Are these the same? – Chrispresso Apr 20 '20 at 16:05
  • thank you. yes they are they same. in terminal its ```/home/paul/anaconda3/bin/python``` and in vscode the current is ~/anaconda3/bin/python – Paul Schal Apr 20 '20 at 16:12
  • Is this being done on a remote environment? You may need to specify the `$DISPLAY` like so when running: `DISPLAY=":0" python helper.py`. You can verify by doing `ctrl + shift + p` and typing `terminal: create new integrated terminal` and specifically running the script – Chrispresso Apr 20 '20 at 16:23
  • many thanks, it works when i use ```DISPLAY=":0" python helper.py``` . – Paul Schal Apr 20 '20 at 16:55
  • Still this seems to be very roundabout. Do you somehow know i dont have always write the DISPLAY=":0"? – Paul Schal Apr 20 '20 at 16:59
  • 1
    It could be that `$DISPLAY` was set incorrectly. This could be tested by doing `echo $DISPLAY` in the different terminals to see what they were set to. This is much more of an Xlib thing than it is a programming thing. So don't get too discouraged ;) – Chrispresso Apr 20 '20 at 17:06

1 Answers1

1

I understand that you are trying to write a script using Python and that if you run it with Terminal it works but if you run it inside the VS Editor it doesn't work! If this is what you say, there can be a difference in the default version that works on the terminal, and between the one that works on the VS, it may be the one that works on the terminal python 3, and the one that works in the editor is python 2. I prefer that you try the development on the spyder program for python You can install it by using this command : sudo apt update && sudo apt install spyder && sudo pip install spyder

and you need add this line in start script if you typing python 2 add "#!/bin/python2" if you typing python 3 add "#!/bin/python3" To tell bash what is version of python you need to run and Keep you learning bro

  • Thank you for the answer. Yes i also use spider and it works in spider. Its just that i really have no clue why it doesnt work in VS Code. It doesnt work even if i use the build-in terminal from VSC and type ```home/paul/anaconda3/bin/python /home/paul/Programming/statFit2/helper.py``` Now if i type exactly this in a normal ubuntu terminal it works. And since i am giving the exact path to the interpreter it should always be the same? – Paul Schal Apr 20 '20 at 16:30
  • It's not necessarily the same because this all has to do with your shell and its environment. If the terminal you are using in Ubuntu is setting `DISPLAY` explicitly then that wouldn't carry over to the terminal in VS Code. You can try setting ` "terminal.integrated.inheritEnv": false` to see if that fixes it. As for how to fix it, the quick-and-dirtry way is to do it in your code: `if not 'DISPLAY' in os.environ: os.environ["DISPLAY"] = ":0"`. – Brett Cannon Apr 21 '20 at 21:36