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, ie 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
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.
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, ie print your clipboard content. Similarly I can copy / paste back to Windows.
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.
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()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.