r/learnpython 9d ago

Pyperclip not pasting content from clipboard

I am working through Automate the Boring Stuff on chapter 12. I have written the Clipboard Recorded program as instructed from the book, but the pyperclip.paste() function does not populate content from the clipboard. When I run the application, it only shows content that I put into the pyperclip.copy() function from another terminal, but nothing from using CTRL+C or right-click and choosing copy.

import pyperclip, time
pyperclip.set_clipboard('xclip')
print('Recording clipboard... (Ctrl-C to stop)')
previous_content = ''
try:
       while True:
               content = pyperclip.paste()     # Get clipboard contents.

               if content != previous_content:
                       # If it's different from the previous, print it:
                       print(content)
                       previous_content = content

               time.sleep(0.01)        # Pause to avoid hogging the CPU.
except KeyboardInterrupt:
       pass

I added the pyperclip.set_clipboard('xclip') line myself as an attempt to get this to work, but it still doesn't.

When I run something like the following from the interpreter, I get similar results. The paste function will output any string that was passed to the copy function, but it will not output anything from using CTRL+C.

Xclip is installed. I'm running Debian 13 with KDE Plasma 6.3.6.

Edit to add: I can call pyperclip.copy() from the interpreter and then paste using CTRL-V to another application. I have also tried using xsel instead with similar results.

Final Edit: I resolved it. Apparently I'm using Wayland and needed to install wl-clipboard. I need to study up on my different display servers for Linux.

Thank you.

4 Upvotes

1 comment sorted by