'Error raised while installing pynput in termux

I'm trying to install pynput in Termux to be able to run my python script. When I try [as in the attached screenshot] pip install pynput I get this error message:

ERROR: Command errored out with exit status 1:
     command: /data/data/com.termux/files/usr/bin/python3 -u -c 'import io, os, sys, setuptools, tokenize; sys.argv[0] = '"'"'/data/data/com.termux/files/usr/tmp/pip-install-f07stt3a/evdev_9d15119a1c774b69be29371e48a8012d/setup.py'"'"'; __file__='"'"'/data/data/com.termux/files/usr/tmp/pip-install-f07stt3a/evdev_9d15119a1c774b69be29371e48a8012d/setup.py'"'"';f = getattr(tokenize, '"'"'open'"'"', open)(__file__) if os.path.exists(__file__) else io.StringIO('"'"'from setuptools import setup; setup()'"'"');code = f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record /data/data/com.termux/files/usr/tmp/pip-record-u2bumfem/install-record.txt --single-version-externally-managed --compile --install-headers /data/data/com.termux/files/usr/include/python3.9/evdev
         cwd: /data/data/com.termux/files/usr/tmp/pip-install-f07stt3a/evdev_9d15119a1c774b69be29371e48a8012d/
    Complete output (37 lines):
    running install
    running build
    running build_py
    creating build
    creating build/lib.linux-aarch64-3.9
    creating build/lib.linux-aarch64-3.9/evdev
    copying evdev/__init__.py -> build/lib.linux-aarch64-3.9/evdev
    copying evdev/device.py -> build/lib.linux-aarch64-3.9/evdev
    copying evdev/ecodes.py -> build/lib.linux-aarch64-3.9/evdev
    copying evdev/eventio.py -> build/lib.linux-aarch64-3.9/evdev
    copying evdev/eventio_async.py -> build/lib.linux-aarch64-3.9/evdev
    copying evdev/events.py -> build/lib.linux-aarch64-3.9/evdev
    copying evdev/evtest.py -> build/lib.linux-aarch64-3.9/evdev
    copying evdev/ff.py -> build/lib.linux-aarch64-3.9/evdev
    copying evdev/genecodes.py -> build/lib.linux-aarch64-3.9/evdev
    copying evdev/uinput.py -> build/lib.linux-aarch64-3.9/evdev
    copying evdev/util.py -> build/lib.linux-aarch64-3.9/evdev
    running build_ext
    running build_ecodes
    The 'linux/input.h' and 'linux/input-event-codes.h' include files
    are missing. You will have to install the kernel header files in
    order to continue:

        yum install kernel-headers-$(uname -r)
        apt-get install linux-headers-$(uname -r)
        emerge sys-kernel/linux-headers
        pacman -S kernel-headers

    In case they are installed in a non-standard location, you may use
    the '--evdev-headers' option to specify one or more colon-separated
    paths. For example:

        python setup.py \
          build \
          build_ecodes --evdev-headers path/input.h:path/input-event-codes.h \
          build_ext --include-dirs  path/ \
          install
    ----------------------------------------
ERROR: Command errored out with exit status 1: /data/data/com.termux/files/usr/bin/python3 -u -c 'import io, os, sys, setuptools, tokenize; sys.argv[0] = '"'"'/data/data/com.termux/files/usr/tmp/pip-install-f07stt3a/evdev_9d15119a1c774b69be29371e48a8012d/setup.py'"'"'; __file__='"'"'/data/data/com.termux/files/usr/tmp/pip-install-f07stt3a/evdev_9d15119a1c774b69be29371e48a8012d/setup.py'"'"';f = getattr(tokenize, '"'"'open'"'"', open)(__file__) if os.path.exists(__file__) else io.StringIO('"'"'from setuptools import setup; setup()'"'"');code = f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record /data/data/com.termux/files/usr/tmp/pip-record-u2bumfem/install-record.txt --single-version-externally-managed --compile --install-headers /data/data/com.termux/files/usr/include/python3.9/evdev Check the logs for full command output.

I am not aware of what to do next. Please help.



Solution 1:[1]

You get error due Termux not having kernel header files which is needed for evdev. Without it it is impossible to install pynput and other modules which require evdev. Regarding your question about command to install it error message already contains it:

    yum install kernel-headers-$(uname -r)
    apt-get install linux-headers-$(uname -r)
    emerge sys-kernel/linux-headers
    pacman -S kernel-headers

BUT DON'T TRY IT

I am not saying that something bad will happen. It just won't work. There are no kernel header files in Termux's repositories and as far as I know only option to install them without risks is to use proot. So you need to do this:

apt update # Update package repositor(y|ies)
apt upgrade # Upgrade everything just in case
apt install proot # Install **proot**
apt install proot-distro # [OPTIONAL] Install official (made by Termux's developer) proot installer

If you installed proot-distro then do this: proot-distro list # List current available distros Then

  1. Install distro which you want
  2. Start it
  3. Install python (and what you need) again using that distro's install commands since proot is like Virtual Machine
  4. Install kernel header files. CHOOSE COMMAND ACCORDING YOUR PROOT DISTRO
  5. Run pip install pynput

Solution 2:[2]

I had the same error message installing evdev for a different package (quantconnect's "lean"); installing the prerequisites fixed the issue. From the man page (I quote debian but pick your distribution):

The latest stable version of python-evdev can be installed from pypi, provided that you have gcc/clang, pip and the Python and Linux development headers installed on your system. Installing them is distribution specific and typically falls in one of the following categories:

   On a Debian compatible OS:

      $ apt-get install python-dev python-pip gcc
      $ apt-get install linux-headers-$(uname -r)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 UltraStudioLTD
Solution 2 Entropic Thunder