如何在Docker容器中使用'keyboard'python模块而不会出现错误?

时间:2019-09-08 22:28:31

标签: python python-3.x docker keyboard

我正在尝试在docker容器中使用python模块 keyboard 进行模拟键盘按键的自动化测试。但是,此模块在docker容器中不断返回错误,而在ubuntu VM中运行良好。

我安装了不同的常用docker容器和不同版本的python。我犯了同样的错误。可悲的是,谷歌并没有像平常一样有用。

以下是我所做操作的易于复制的版本:

docker run -t -d -u root --name docker_container python:3.7
echo $(docker exec -u root docker_container bash -c 'python3 -m keyboard' )

这将返回以下错误消息:

/usr/local/lib/python3.7/site-packages/keyboard/_nixkeyboard.py:110: UserWarning: Failed to create a device file using `uinput` module. Sending of events may be limited or unavailable depending on plugged-in devices.
  device = aggregate_devices('kbd')
Exception in thread Thread-1:
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/threading.py", line 926, in _bootstrap_inner
    self.run()
  File "/usr/local/lib/python3.7/threading.py", line 870, in run
    self._target(*self._args, **self._kwargs)
  File "/usr/local/lib/python3.7/site-packages/keyboard/_nixcommon.py", line 106, in start_reading
    self.event_queue.put(device.read_event())
  File "/usr/local/lib/python3.7/site-packages/keyboard/_nixcommon.py", line 83, in read_event
    data = self.input_file.read(struct.calcsize(event_bin_format))
AttributeError: 'NoneType' object has no attribute 'read'

Exception in thread Thread-2:
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/threading.py", line 926, in _bootstrap_inner
    self.run()
  File "/usr/local/lib/python3.7/threading.py", line 870, in run
    self._target(*self._args, **self._kwargs)
  File "/usr/local/lib/python3.7/site-packages/keyboard/_nixcommon.py", line 106, in start_reading
    self.event_queue.put(device.read_event())
  File "/usr/local/lib/python3.7/site-packages/keyboard/_nixcommon.py", line 83, in read_event
    data = self.input_file.read(struct.calcsize(event_bin_format))
AttributeError: 'NoneType' object has no attribute 'read'

Traceback (most recent call last):
  File "/usr/local/lib/python3.7/runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "/usr/local/lib/python3.7/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/usr/local/lib/python3.7/site-packages/keyboard/__main__.py", line 10, in <module>
    keyboard.hook(print_event_json)
  File "/usr/local/lib/python3.7/site-packages/keyboard/__init__.py", line 459, in hook
    append(callback)
  File "/usr/local/lib/python3.7/site-packages/keyboard/_generic.py", line 67, in add_handler
    self.start_if_necessary()
  File "/usr/local/lib/python3.7/site-packages/keyboard/_generic.py", line 35, in start_if_necessary
    self.init()
  File "/usr/local/lib/python3.7/site-packages/keyboard/__init__.py", line 194, in init
    _os_keyboard.init()
  File "/usr/local/lib/python3.7/site-packages/keyboard/_nixkeyboard.py", line 114, in init
    build_tables()
  File "/usr/local/lib/python3.7/site-packages/keyboard/_nixkeyboard.py", line 73, in build_tables
    dump = check_output(['dumpkeys', '--keys-only'], universal_newlines=True)
  File "/usr/local/lib/python3.7/subprocess.py", line 395, in check_output
    **kwargs).stdout
  File "/usr/local/lib/python3.7/subprocess.py", line 472, in run
    with Popen(*popenargs, **kwargs) as process:
  File "/usr/local/lib/python3.7/subprocess.py", line 775, in __init__
    restore_signals, start_new_session)
  File "/usr/local/lib/python3.7/subprocess.py", line 1522, in _execute_child
    raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: 'dumpkeys': 'dumpkeys'

据我了解,它无法访问uinput设备/键盘以转储键。有没有一种方法可以虚拟模拟容器中的键盘以使其正常工作?

1 个答案:

答案 0 :(得分:0)

尝试安装以下内容:

apt-get install -y kmod kbd

因为您需要this才能获得dumpkeys