简体   繁体   English

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

[英]How do I use the 'keyboard' python module in a docker container without an error?

I am trying to use the python module keyboard in a docker container for testing automation that emulates keyboard presses. 我正在尝试在docker容器中使用python模块键盘进行模拟键盘按键的自动化测试。 However, this module continually returns an error in the docker container whereas it works fine in an ubuntu VM. 但是,此模块在docker容器中不断返回错误,而在ubuntu VM中运行良好。

I have installed different commonly used docker containers and different versions of python. 我安装了不同的常用Docker容器和不同版本的python。 I get the same error. 我犯了同样的错误。 Sadly, google hasn't been as helpful as it usually is here. 可悲的是,谷歌并没有像平常一样有用。

Here's an easily reproducible version of what I've done: 这是我所做操作的易于复制的版本:

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

This returns the below error message: 这将返回以下错误消息:

/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'

From what I understand, it can't access a uinput device / keyboard to be able to dump the keys. 据我了解,它无法访问uinput设备/键盘以转储键。 Is there a way to virtually simulate a keyboard in the container to allow this to work? 有没有一种方法可以虚拟模拟容器中的键盘以使其工作?

try to install the following: 尝试安装以下内容:

apt-get install -y kmod kbd

since you need this to get dumpkeys 因为你需要这个来获取dumpkeys

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM