Pyperclip is giving an error
Asked Answered
S

3

8

I use python 2.7. I installed pyperclip using sudo pip install pyperclip and it was installed successfully.

Every time I use the following simple code

import pyperclip
pyperclip.copy('Hello World')
message=pyperclip.paste()
print (message)

I get the following error:

/usr/lib/python2.7/dist-packages/gtk-2.0/gtk/__init__.py:57:
GtkWarning: could not open display   warnings.warn(str(e),
_gtk.Warning) /usr/local/lib/python2.7/dist-packages/pyperclip/__init__.py:102:
GtkWarning: IA__gtk_clipboard_get_for_display: assertion 'display !=
NULL' failed   cb = gtk.Clipboard() Traceback (most recent call last):
File "first.py", line 401, in <module>
    pyperclip.copy('Hello World')   File "/usr/local/lib/python2.7/dist-packages/pyperclip/__init__.py", line
102, in _copyGtk
    cb = gtk.Clipboard() RuntimeError: could not create GtkClipboard object
Spleeny answered 23/8, 2015 at 5:26 Comment(2)
Don't use sudo pip; either use a virtualenv or pip install --user.Pauli
Do you have pygtk installed?Induplicate
F
9

It always helps to read the documentation.

On Windows, no additional modules are needed.
On Mac, the module uses pbcopy and pbpaste, which should come with the os.
On Linux, install xclip or xsel via package manager. For example, in Debian:
sudo apt-get install xclip

Otherwise on Linux, you will need the gtk or PyQt4 modules installed.

gtk and PyQt4 modules are not available for Python 3, and this module does not work with PyGObject yet.

So all you need to do at your terminal is sudo apt install xclip and then the gtk and PyQt4 modules via pip (for python 2).

Fogbow answered 8/7, 2017 at 23:46 Comment(0)
A
3

When using Python3 on a Debian based system, please install xclip and pyqt4 as the documentation states.

sudo apt-get install xclip python3-pyqt4

Then you can easily copy DataFrames like this:

import pandas as pd
import numpy as np
import sys

dates = pd.date_range('20130101',periods=6)
df = pd.DataFrame(np.random.randn(6,4),index=dates,columns=list('ABCD'))

df.to_clipboard()

Pasting works with Google Sheets or Excel.

Amimia answered 1/8, 2018 at 15:56 Comment(0)
A
1

None of these work on ubuntu server - 16.04. Reason being ubuntu server does not have a GUI. Reference link : https://www.thetopsites.net/article/51662213.shtml

Archaic answered 12/10, 2020 at 9:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.