Home > Software engineering >  Mouse, and keyboard python modules conflicting?
Mouse, and keyboard python modules conflicting?

Time:09-27

I am writing a test program which contains the mouse, and keyboard modules together, but they conflict when I run the program.

I am new-ish to making posts, so please forgive my ignorance to question posts.

If applicable, I run Linux Mint 20.04 (Uma).

Here is my code:

import keyboard
import mouse
import time

time.sleep(2)
print("Finished part 1!")

x, y = mouse.get_position()

time.sleep(2)
print("Finished part 2!")

mouse.move(x, y)

time.sleep(2)
print("Finished part 3!")

keyboard.add_hotkey('ctrl alt c', mouse.move(x, y))

If I run this program normally, such as /bin/python3 /home/bhrz/Scripts/Python/Mouse/main.py in my terminal, it outputs:

Finished part 1!
Finished part 2!
Finished part 3!
Traceback (most recent call last):
  File "/home/bhrz/Scripts/Python/Mouse/main.py", line 18, in <module>
    keyboard.add_hotkey('ctrl alt c', mouse.move(x, y))
  File "/usr/local/lib/python3.8/dist-packages/keyboard/__init__.py", line 639, in add_hotkey
    _listener.start_if_necessary()
  File "/usr/local/lib/python3.8/dist-packages/keyboard/_generic.py", line 35, in start_if_necessary
    self.init()
  File "/usr/local/lib/python3.8/dist-packages/keyboard/__init__.py", line 196, in init
    _os_keyboard.init()
  File "/usr/local/lib/python3.8/dist-packages/keyboard/_nixkeyboard.py", line 113, in init
    build_device()
  File "/usr/local/lib/python3.8/dist-packages/keyboard/_nixkeyboard.py", line 109, in build_device
    ensure_root()
  File "/usr/local/lib/python3.8/dist-packages/keyboard/_nixcommon.py", line 174, in ensure_root
    raise ImportError('You must be root to use this library on linux.')
ImportError: You must be root to use this library on linux.

When I attempt to solve that error by entering this, sudo /bin/python3 /home/bhrz/Scripts/Python/Mouse/main.py, it outputs:

Traceback (most recent call last):
  File "/home/bhrz/Scripts/Python/Mouse/main.py", line 2, in <module>
    import mouse
ModuleNotFoundError: No module named 'mouse'

I have looked for answers, and some were to do the above, which as you can see, has resulted in failure, and another said to do sudo su then run the script--which also failed, with the same output as above.

Please help me figure out what the problem is.

I do have unintentionally 3 versions of python installed: 2.7, 3.8.10, and 3.9.5. I personally installed Python 3.9.5. I don't use 2.7, but it was installed with the python-dev

On Python 3.9.5, the keyboard module isn't recognized, but on Python 3.8.10, it is.

Thank you!

CodePudding user response:

Use sudo /bin/python3.8 /home/bhrz/Scripts/Python/Mouse/main.py as you said:

On Python 3.9.5, the keyboard module isn't recognized, but on Python 3.8.10, it is.

python3 has been replaced when you installed python3.9.5. So it belongs to python 3.9.5. You need to use python3.8 in order to execute your script in python 3.8.10 environment.

CodePudding user response:

I have found the answer to my problem. The thread: Python can't find module when started with sudo 's second answer fixed my problem. Instead of running sudo python3(/3.8) myScriptName.py, run sudo -E python3(/3.8) myScriptName.py.

Thank you, Nima for attempting to help me!

  • Related