0

pyperclip is failing to determine the clipboard for my system

any advice?

I am running CentOS 7

I have verified xclip and is working

cat /etc/redhat-release
CentOS Linux release 7.6.1810 (Core)

% echo "test" | xclip -i -sel clip
% echo "test selection" | xclip -i -sel clip
% xclip -o -sel clip
test selection

In [1]: import pyperclip
In [2]: pyperclip.copy("test selection")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/data/py36_venv/lib64/python3.6/site-packages/pyperclip/__init__.py", line 658, in lazy_load_stub_copy
    copy, paste = determine_clipboard()
  File "/home/data/py36_venv/lib64/python3.6/site-packages/pyperclip/__init__.py", line 568, in determine_clipboard
    os.environ["XDG_SESSION_TYPE"] == "wayland" and
  File "/usr/lib64/python3.6/os.py", line 669, in __getitem__
    raise KeyError(key) from None
KeyError: 'XDG_SESSION_TYPE'

'XDG_SESSION_TYPE'


1 Answers1

0

If you are running as root, then this may happen because pyperclip requires a environment variable XDG_SESSION_TYPE to be set. The value of XDG_SESSION_TYPE depends on the X setup you have.

To resolve, from a non-root user get the XDG_SESSION_TYPE value:

% echo $XDG_SESSION_TYPE

Within the session running as root, set the env var before running the python script:

% export XDG_SESSION_TYPE=<value_from_user_account>
Timothy C. Quinn
  • 3,739
  • 1
  • 35
  • 47
  • One viable value is to set `export XDG_SESSION_TYPE=x11`. This works in my setup where I access a Jupterlab session hosted on a linux machine. To get the environmental variable to refresh, I ended up restarting the Jupyter server after updating the `.zshrc` file. – llinfeng Feb 17 '21 at 23:54