Reputation: 21
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.
Upvotes: 2
Views: 6427
Reputation: 11
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
Upvotes: 1