Search code examples
pythonpynput

Import error: cannot import name '_NORMALIZED_MODIFIERS' from pynput


I can't find anything on how to fix this import error with pynput. Reinstalled pynput, made a simple test, tried different keys to press/release and all same error.

Error msg:

PS C:\Users\username\Desktop\Python Voice Cmd Input> python -m test
Traceback (most recent call last):
  File "C:\ProgramData\Miniconda3\lib\runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "C:\ProgramData\Miniconda3\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "C:\Users\username\Desktop\Python Voice Cmd Input\test.py", line 6, in <module>
    keyboard.press(Key.space)
  File "C:\ProgramData\Miniconda3\lib\site-packages\pynput\keyboard\_base.py", line 363, in press
    self._update_modifiers(resolved, True)
  File "C:\ProgramData\Miniconda3\lib\site-packages\pynput\keyboard\_base.py", line 574, in _update_modifiers
    if self._as_modifier(key):
  File "C:\ProgramData\Miniconda3\lib\site-packages\pynput\keyboard\_base.py", line 595, in _as_modifier
    from .. import _NORMALIZED_MODIFIERS
ImportError: cannot import name '_NORMALIZED_MODIFIERS' from 'pynput' (C:\ProgramData\Miniconda3\lib\site-packages\pynpu
t\__init__.py)

Does not return error:

$ Python
>>> import pynput   

and typing the script from IDLE returns same error.

Versions:
python 3.7.4
pip 19.3.1
conda 4.8.0 (miniconda)
pynput 1.6.1
Windows 8.1

test.py

#!/usr/bin/env python3
from pynput.keyboard import Key, Controller

keyboard = Controller()

keyboard.press(Key.space)
keyboard.release(Key.space)

Solution

  • This is an error in the 1.6.1 release of pynput. you can reinstall a newer version or In the file '_base' in \pynput\keyboard\ it tries to execute:

    from .. import _NORMALIZED_MODIFIERS
    

    All you need to do is change that too:

    from . import _NORMAL_MODIFIERS
    

    This worked for me, hope it helps :)