问题运行pynput模块在谷歌协作



我正在尝试安装和导入使用google colab的pynput模块。然而,虽然我设法安装使用"!pip安装pynput",当我导入模块时,如:

from pynput.keyboard import Key, Listener

我得到下面的错误:

---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-13-2da480c0a0bb> in <module>
----> 1 from pynput.keyboard import Key, Listener
2 
3 # def show(key):
4 
5 #     print('nYou Entered {0}'.format( key))
2 frames
/usr/local/lib/python3.7/dist-packages/pynput/_util/__init__.py in backend(package)
80                 ' * {}'.format(s)
81                 for s in resolutions))
---> 82             if resolutions else '')
83 
84 
ImportError: this platform is not supported: ('failed to acquire X connection: Bad 
display name ""', DisplayNameError(''))
Try one of the following resolutions:
* Please make sure that you have an X server running, and that the DISPLAY 
environment variable is set correctly
---------------------------------------------------------------------------
NOTE: If your import is failing due to a missing package, you can
manually install dependencies using either !pip or !apt.
To view examples of installing some common dependencies, click the
"Open Examples" button below.
---------------------------------------------------------------------------

我可以使用哪些代码安装模块和使用它与谷歌协作?

Google colab运行在Google Cloud中的机器实例上,因此python无法获得对键盘/监视器/鼠标的控制。简而言之,开发人员通过web浏览器(google chrome, mozilla等)与google协作。

长话短说,你试图通过在云上运行代码来控制本地硬件,这在这种情况下通常是不可能的。

所以如果你想执行这种类型的实验,那就在你的本地机器上做,使用任何IDE(pycharm等)。

你可以检查一下:在这里输入链接描述

如果你想在实验室里做实验,那就使用本地运行时

你可以检查这个详细的实现:在这里输入链接描述

最新更新