Skip to content

Instantly share code, notes, and snippets.

@plembo
Last active April 15, 2024 18:23
Show Gist options
  • Save plembo/6a035299f50db092ab710c74eaf6dcfb to your computer and use it in GitHub Desktop.
Save plembo/6a035299f50db092ab710c74eaf6dcfb to your computer and use it in GitHub Desktop.
Linux workaround for pyperclip.copy

Linux workaround for pyperclip.copy

Al Sweigart's pyperclip library for Python attempts to provide cross-platform methods for interacting with the clipboard on a computer desktop. I and others have found that the .copy method does not work with our Linux desktops (mine is currently Ubuntu 18.04 LTS with the default Gnome Shell).

A workaround was identified gkotian in an issue opened on the project's Github repo.

Here's the snippet provided by gkotian, which I used in a project from Al's Automate the Boring Stuff with Python book.:

import os

text = 'The text to be copied to the clipboard.'
os.system("echo '{}' | xclip -selection clipboard".format(text))

This workaround requires installation of astrand's xclip program (installed on Ubuntu with "sudo apt install xclip").

@Lu251970
Copy link

Hi !. I've been trying to improve my skills in python by reading and doing the exercises proposed in Al Sweigart's book. I use Debian and there was an issue on the pyperclip module while running the codes for Adding Bullets to Wiki Markup and Password Locker. The solution proposed by gkotian was successfully implemented in both codes and performed as expected. However, I would like to inquire whether it is feasible to have xclip copy the text from the clipboard instead of storing it in a variable, and then perform the modifications and resend it back to the clipboard. Thank you! Luciana.

@plembo
Copy link
Author

plembo commented Apr 15, 2024

Sorry for the late reply. I really wouldn't know where to begin to answer you question. As this is merely a (very) low traffic gist archive, it's unlikely anyone else will even see it. I'd suggest going to https://github.com/asweigart/pyperclip/issues to see if anyone there can help. Also, it looks like pyperclip is no longer supported, so you might want to look for an alternative like this: https://github.com/NikitaBeloglazov/clipman.

@Lu251970
Copy link

Sorry for the late reply. I really wouldn't know where to begin to answer you question. As this is merely a (very) low traffic gist archive, it's unlikely anyone else will even see it. I'd suggest going to https://github.com/asweigart/pyperclip/issues to see if anyone there can help. Also, it looks like pyperclip is no longer supported, so you might want to look for an alternative like this: https://github.com/NikitaBeloglazov/clipman.

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment