在Google Colab中运行pynput模块出现问题。

9 浏览
0 Comments

在Google Colab中运行pynput模块出现问题。

我正在尝试在Google Colab中安装和导入pynput模块。然而,尽管我成功使用"!pip install pynput"安装了该模块,但当我导入模块时,例如:

from pynput.keyboard import Key, Listener

我遇到了以下错误:

---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
 in 
----> 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中使用它?

0
0 Comments

问题:在Google Colab中运行pynput模块出现问题的原因是Google Colab运行在Google Cloud的机器实例上,因此Python无法控制键盘/显示器/鼠标。简而言之,开发者通过Web浏览器(如Google Chrome、Mozilla等)与Google Colab进行交互。

解决方法:如果您想执行此类实验,请在本地机器上使用任何IDE(如PyCharm等)。如果您仍然想在Colab中进行实验,请使用本地运行时。

您可以查看这个链接了解详细实现:enter link description here

0