我正在Windows 10 build 20190上通过Windows Terminal Preview(1.2.2234.0)在WSL2中运行的Ubuntu 18.04上使用IPython 7.16.1(Python 3.7.7)(尽管问题不仅限于IPython,但问题在于外壳本身)。我正在尝试对从Windows(即WSL外部)复制的数据使用pandas.read_clipboard()
。但是,出现以下错误:
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
我确实知道这是由于WSL不支持显示器,但是,由于我可以与WSL和Windows之间复制粘贴数据,因此应该有一种机制可以访问Windows剪贴板。有解决办法吗?
我按照建议的here和here看了xclip
,xsel
,QTpy
并没有帮助。
完整堆栈跟踪:
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
答案 0 :(得分:1)
如果您通过旧的cmd.exe(假设x <- 1:4
y <- c(1,2,3.000000001,4)
位于%LOCALAPPDATA%\Microsoft\WindowsApps
中)通过以下方式启动Ubuntu(从Windows Store,可以获取注释20.04)
PATH
(不带 > start ubuntu1804
,您可以留在cmd.exe中-可能但不建议使用。)它在Linux终端中运行Ubuntu。
在那跑(最好的一次)
start
应该可以工作,即打印剪贴板内容。 同样,我可以复制/粘贴回Windows。
答案 1 :(得分:0)
如果目标是从WSL Shell中获取Windows剪贴板内容,则可以通过powershell(在WSL中可用)进行操作:
powershell.exe Get-Clipboard
如果您尝试从WSL中运行的 python 中访问此文件,则需要使用subprocess.Popen()之类的命令来运行上面的命令。
答案 2 :(得分:0)
我注意到该问题与以下代码段有关:pandas/io/clipboard/init.py#L523-L526
如果我编辑行if "Microsoft" in f.read():
,并将“ Microsoft”替换为“ microsoft”(小写的“ m”),则剪贴板功能对我有用。
这不是一个好的长期解决方案,但在熊猫团队将其集成之前,绝对是一个简单的补丁。
答案 3 :(得分:0)
截至今天,运行 Pandas 1.2.3 这仍然是一个问题。
我正在使用的一个简单的解决方法可能对其他人有帮助:
在 Windows 中将一些结构化数据复制到剪贴板后
import pandas as pd
import pyperclip
pd.read_csv(io.StringIO(pyperclip.paste()), sep='\t')
这与 pd.read_clipboard() 给出的结果相同