3

I am trying to use the inputs library to get user input from mice, gamepads, and keyboards.

I tried the following code which is supposed to read events from all devices:

import inputs

while True:
    for device in inputs.devices:
        for event in device.read():
            print(event)

There is a problem when I run the code - I get the following error: AttributeError: 'NoneType' object has no attribute 'terminate'

I have also tried to read a single event:

import inputs

while True:
    for device in inputs.devices:
        event = device.read()
        print(event)

This gives me the same error.

I am using Python3.6 and inputs==0.4 from pip

Does anyone know how to fix this error?

Full traceback:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "C:\python36\lib\multiprocessing\spawn.py", line 105, in spawn_main
    exitcode = _main(fd)
  File "C:\python36\lib\multiprocessing\spawn.py", line 114, in _main
    prepare(preparation_data)
  File "C:\python36\lib\multiprocessing\spawn.py", line 225, in prepare
    _fixup_main_from_path(data['init_main_from_path'])
  File "C:\python36\lib\multiprocessing\spawn.py", line 277, in _fixup_main_from_path
    run_name="__mp_main__")
  File "C:\python36\lib\runpy.py", line 263, in run_path
    pkg_name=pkg_name, script_name=fname)
  File "C:\python36\lib\runpy.py", line 96, in _run_module_code
    mod_name, mod_spec, pkg_name, script_name)
  File "C:\python36\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "C:\Users\David\Documents\GitHub\Bubbles\testing.py", line 5, in <module>
    event = device.read()
  File "C:\python36\lib\site-packages\inputs.py", line 2313, in read
    return next(iter(self))
  File "C:\python36\lib\site-packages\inputs.py", line 2273, in __iter__
    event = self._do_iter()
  File "C:\python36\lib\site-packages\inputs.py", line 2292, in _do_iter
    data = self._get_data(read_size)
  File "C:\python36\lib\site-packages\inputs.py", line 2365, in _get_data
    return self._pipe.recv_bytes()
  File "C:\python36\lib\site-packages\inputs.py", line 2330, in _pipe
    self._listener.start()
  File "C:\python36\lib\multiprocessing\process.py", line 105, in start
    self._popen = self._Popen(self)
  File "C:\python36\lib\multiprocessing\context.py", line 223, in _Popen
    return _default_context.get_context().Process._Popen(process_obj)
  File "C:\python36\lib\multiprocessing\context.py", line 322, in _Popen
    return Popen(process_obj)
  File "C:\python36\lib\multiprocessing\popen_spawn_win32.py", line 33, in __init__
    prep_data = spawn.get_preparation_data(process_obj._name)
  File "C:\python36\lib\multiprocessing\spawn.py", line 143, in get_preparation_data
    _check_not_importing_main()
  File "C:\python36\lib\multiprocessing\spawn.py", line 136, in _check_not_importing_main
    is not going to be frozen to produce an executable.''')
RuntimeError:
        An attempt has been made to start a new process before the
        current process has finished its bootstrapping phase.

        This probably means that you are not using fork to start your
        child processes and you have forgotten to use the proper idiom
        in the main module:

            if __name__ == '__main__':
                freeze_support()
                ...

        The "freeze_support()" line can be omitted if the program
        is not going to be frozen to produce an executable.
Exception ignored in: <bound method InputDevice.__del__ of inputs.Keyboard("/dev/input/by-id/usb-A_Nice_Keyboard-event-kbd")>
Traceback (most recent call last):
  File "C:\python36\lib\site-packages\inputs.py", line 2337, in __del__
  File "C:\python36\lib\multiprocessing\process.py", line 116, in terminate
AttributeError: 'NoneType' object has no attribute 'terminate'
David Callanan
  • 5,601
  • 7
  • 63
  • 105
  • Possible duplicate of [Python: Attribute Error - 'NoneType' object has no attribute 'something'](https://stackoverflow.com/questions/8949252/python-attribute-error-nonetype-object-has-no-attribute-something) – ech0 Aug 13 '18 at 11:38
  • 1
    Could you please include the full traceback of the error message? – mkrieger1 Aug 13 '18 at 11:39
  • your code works for me if I am in "sudo" otherwise I have permission denied. you should describe more the error – p.deman Aug 13 '18 at 11:52
  • @p.deman I will try that – David Callanan Aug 13 '18 at 13:03
  • @mkrieger1 done – David Callanan Aug 13 '18 at 13:03
  • @p.deman I am using windows and I am running cmd as administrator and then executing the python script `python3 testing.py`. This is still not working. – David Callanan Aug 13 '18 at 13:19
  • ok. I haven't windows computer accessible for now. it seems that the multiprocesing library is involved, which version do you have ? maybe post your problem here as well: https://github.com/zeth/inputs/issues – p.deman Aug 13 '18 at 13:44
  • I tried on a windows computer (python 3.6.3 and inputs version 0.4) and it works fine – p.deman Aug 21 '18 at 11:30

1 Answers1

0

Define your steps under a method like:

def func():
    while True:
        for device in inputs.devices:
        event = device.read()
        print(event)

And then call your function using:

if __name__ == '__main__':
     func()
Elletlar
  • 3,136
  • 7
  • 32
  • 38