2

I am trying to install and import the pynput module using google colab. However, although I managed to install using "!pip install pynput", when I import the module such as:

from pynput.keyboard import Key, Listener

I get the below error:

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

which code can I use the install the module and use it with google colab?

  • Does this answer your question? [How do I install Python packages in Google's Colab?](https://stackoverflow.com/questions/51342408/how-do-i-install-python-packages-in-googles-colab) – kotatsuyaki Nov 21 '22 at 10:54
  • No, it does not answer – ForeverLearner Nov 21 '22 at 15:17
  • My bad, didn't read the error message throughly before flagging; your problem has nothing to do with package installation. As per [the documentation of `pynput`](https://pynput.readthedocs.io/en/latest/), it supports selecting backends via the `PYNPUT_BACKEND` environment variable. Since there isn't a Xorg server running on Colab (which is the cause of the error), maybe you can try the `dummy` backend. The documentations says that it's "non-functional but importable" though, so I doubt whether it's actually useful. – kotatsuyaki Nov 21 '22 at 15:22

1 Answers1

1

Google colab runs on a machine instance in Google Cloud so that python can't able to gain control to the keyboard/monitor/mouse. In a nutshell developers interact with google colab through web browsers (google chrome, mozilla etc).

Long story short you are trying to control local hardware by runing code on cloud, which is in general not possible in this case.

So if you want to perform such type of experiment then do it on your local machine, using any IDE(pycharm etc).

you can check this:enter link description here

Still you want to do experiment in colab then use local runtime

you can check this for detail implementation:enter link description here