1

I am using IPython 7.16.1 (Python 3.7.7) on Ubuntu 18.04 running in WSL2 via Windows Terminal Preview (1.2.2234.0) on Windows 10 build 20190 (though the issue is not limited to IPython, it is with the shell itself). I am trying to use pandas.read_clipboard() on data copied from Windows, i.e. outside WSL. However, getting the following error:

PyperclipException:
    Pyperclip could not find a copy/paste mechanism for your system.
    For more information, please visit
    https://pyperclip.readthedocs.io/en/latest/introduction.html#not-implemented-error

I do understand this is due to WSL not supporting a display, however, since I can copy paste data to and from WSL and Windows, there should be a mechanism where I could access the windows clipboard. Is there a solution to this?

I have looked at xclip, xsel, QTpy as suggested here, and here, didn't help.

Full stack trace:

In [1]: import pandas as pd
In [2]: df = pd.read_clipboard()
---------------------------------------------------------------------------
PyperclipException                        Traceback (most recent call last)
<ipython-input-2-861af318b71b> in <module>
----> 1 df = pd.read_clipboard()

~/anaconda3/envs/tensorflow_gpu/lib/python3.7/site-packages/pandas/io/clipboards.py in read_clipboard(sep, **kwargs)
     36     from pandas.io.parsers import read_csv
     37
---> 38     text = clipboard_get()
     39
     40     # Try to decode (if needed, as "text" might already be a string here).

~/anaconda3/envs/tensorflow_gpu/lib/python3.7/site-packages/pandas/io/clipboard/__init__.py in lazy_load_stub_paste()
    648     global copy, paste
    649     copy, paste = determine_clipboard()
--> 650     return paste()
    651
    652

~/anaconda3/envs/tensorflow_gpu/lib/python3.7/site-packages/pandas/io/clipboard/__init__.py in __call__(self, *args, **kwargs)
    285     class ClipboardUnavailable:
    286         def __call__(self, *args, **kwargs):
--> 287             raise PyperclipException(EXCEPT_MSG)
    288
    289         def __bool__(self) -> bool:

PyperclipException:
    Pyperclip could not find a copy/paste mechanism for your system.
    For more information, please visit
    https://pyperclip.readthedocs.io/en/latest/introduction.html#not-implemented-error
Salvatore
  • 10,815
  • 4
  • 31
  • 69
Sayandip Dutta
  • 15,602
  • 4
  • 23
  • 52
  • **DO NOT post images of code, data, error messages, etc.** - copy or type the text into the question. [ask] – Rob Aug 23 '20 at 01:48
  • @Rob There is no data to post, my code is `pandas.read_clipboard()` , I have specified that. If you need the stack trace in text I can add it, but don't see how that would help. – Sayandip Dutta Aug 23 '20 at 09:37
  • 3
    @SayandipDutta He did mean the stack trace. It is much easier to read and use as text instead of as an image and it is generally good practice to not use images for representing any kind of text. – Salvatore Aug 25 '20 at 19:56

4 Answers4

3

I noticed that the issue had to do with following block of code: pandas/io/clipboard/init.py#L523-L526

If I edit the line if "Microsoft" in f.read():, and replace "Microsoft" with "microsoft" (lowercase "m"), then the clipboard functionality works for me.

Not a good long-term solution, but definitely a simple patch until pandas teams integrates this.

Maxim
  • 725
  • 1
  • 8
  • 24
  • Brilliant! How did you figure it out? I viewed this piece of code, never though of this. – Sayandip Dutta Jan 08 '21 at 18:34
  • 2
    Well I noticed that pyperclip worked fine so I figured it was an error in the pandas code and the inspection lead me to that line. FYI - I have submitted a PR to pandas but it may take a while till it is released. https://github.com/pandas-dev/pandas/pull/38546 – Maxim Jan 09 '21 at 23:17
1

If you start Ubuntu (from Windows Store, note 20.04 is available) from good old cmd.exe (given %LOCALAPPDATA%\Microsoft\WindowsApps is in PATH) via

    > start ubuntu1804

(w/o start you would stay in cmd.exe—possible but not recommendable.) It runs Ubuntu in a Linux terminal. Running there (best in a venv)

    (venv) > pip install pyperclip
    (venv) > python -c "import pyperclip; print(pyperclip.paste())"

should work, i.e. print your clipboard content. Similarly I can copy / paste back to Windows.

Note the orange Ubuntu icon: enter image description here

thoku
  • 1,120
  • 9
  • 27
1

As of today, running Pandas 1.2.3 this is still a problem.

A simple workaround that I am using could be helpful for others:

After copying some structured data to the clipboard in Windows

import pandas as pd
import pyperclip

pd.read_csv(io.StringIO(pyperclip.paste()), sep='\t')

This gives the same result as pd.read_clipboard()

0

If the goal is to get the Windows clipboard contents from a WSL shell, then you can do that via powershell (which is available in WSL):

powershell.exe Get-Clipboard

If you're trying to access this from within python running in WSL, then you'll need to use something like subprocess.Popen() to run the command above.

kerasbaz
  • 1,774
  • 1
  • 6
  • 15
  • Thanks. I know this, not sure how to make it work for this particular use case. `pd.read_clipboard()` is really handy (eventhough I can manage without it), it would be great if I could make it work somehow. – Sayandip Dutta Aug 25 '20 at 18:51
  • So your question is really how to monkey patch pandas? You're dead-set on pd.read_clipboard() delivering the clipboard contents? Is there any particular reason why? – kerasbaz Aug 25 '20 at 20:44