Search code examples
pythonubuntuwindows-subsystem-for-linuxpyperclipwindows-terminal

Pyperclip error on WSL2 running Ubuntu 18 LTS while trying to access data copied from windows


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

Solution

  • 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.