1

I am having some trouble importing some things from pynput library.

In my code I want use the a python library (pynput) to do some actions in the mouse and in keyboard. When I import just the keyboard or the mouse it works but when importing both at some time it give me some errors.

Here is my code:

from pynput.mouse import Button, Controller
from pynput.keyboard import Key, Controller

from time import sleep

mouse = Controller()
keyboard = Controller()
rock1x = 691
rock1y = 466
rock2x = 548
rock2y = 350
rock3x = 687
rock3y = 234

while (1):
    drop1x = 1183
    drop1y = 325
    drop2x = 1220
    drop2y = 325
    drop3x = 1263
    drop3y = 325
    drop4x = 1303
    drop4y = 325

    for i in range(8):
        sleep(2.5)
        mouse.position = (rock2x,rock2y)
        sleep(0.3)
        mouse.press(Button.left)
        mouse.release(Button.left)
        sleep(2.1)

        mouse.position = (rock3x,rock3y)
        sleep(0.3)

        mouse.press(Button.left)
        mouse.release(Button.left)
        sleep(2.1)

        mouse.position = (rock1x,rock1y)

        sleep(0.2)
        mouse.press(Button.left)
        mouse.release(Button.left)

    for i in range(3):
        keyboard.press(Key.shift)
        for x in range(6):
            mouse.position = (drop1x,drop1y)
            sleep(0.3)
            mouse.press(Button.left)
            mouse.release(Button.left) 
            drop1y=drop1y+35
        for x in range(6):
            mouse.position = (drop2x,drop2y)
            sleep(0.3)
            mouse.press(Button.left)
            mouse.release(Button.left) 
            drop2y=drop2y+35
        for x in range(6):
            mouse.position = (drop3x,drop3y)
            sleep(0.3)
            mouse.press(Button.left)
            mouse.release(Button.left) 
            drop3y=drop3y+35
        for x in range(6):
            mouse.position = (drop4x,drop4y)
            sleep(0.3)
            mouse.press(Button.left)
            mouse.release(Button.left) 
            drop4y=drop4y+35
        keyboard.release(Key.shift)
    sleep(3)

When I run this code the follow error show up:

Traceback (most recent call last):
  File "mining.py", line 29, in <module>
    mouse.press(Button.left)
  File "/home/filipe/.local/lib/python2.7/site-packages/pynput/keyboard/_base.py", line 366, in press
    if resolved.is_dead:
AttributeError: 'NoneType' object has no attribute 'is_dead'

But when I comment the second line:

from pynput.keyboard import Key, Controller

the code runs until:

keyboard.press(Key.shift)

and gives me the error:

Traceback (most recent call last):
  File "mining.py", line 48, in <module>
    keyboard.press(Key.shift)
NameError: name 'Key' is not defined

How should I import from pynput library to use the mouse and keyboard?

Lukas
  • 49
  • 2
  • 10
  • 4
    What is the error? What have you tried already? See [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask) – Aviv Shai Sep 08 '18 at 19:27
  • Thanks for the comment, I already edited my question with some new info. – Lukas Sep 08 '18 at 20:31

1 Answers1

4

I think that the problem is that you are importing two different Controllers.

The second one (pynput.keyboard.Controller) overrides the first one since it is the last one defined. Therefore, your variable mouse is actually a pynput.keyboard.Controller object and not a pynput.mouse.Controller object like you expected.

The error happens when you call mouse.press(Button.left) because the Keyboard object is trying to press a Button, which it cannot do (it can only press Keys).

To fix this, import the modules "generally" using import/as instead of importing "specific" parts of them using from/import:

import pynput.mouse    as ms
import pynput.keyboard as kb

This way, you can differentiate between the two controllers:

mouse    = ms.Controller()
keyboard = kb.Controller()

Hope this helps – please respond with any feedback!

Aviv Shai
  • 997
  • 1
  • 8
  • 20
  • Also be sure to change every time you use `Button` to `ms.Button` and `Key` to `kb.Key`. For example, `Button.left` has to be changed to `ms.Button.left`. – Aviv Shai Sep 08 '18 at 21:03
  • 1
    Thanks!! Your solution works. I wrote my code as similar of yours. Now I have 'mouse = pynput.mouse.Controller()' and 'keyboard = pynput.keyboard.Controller()'. I import the library this way: 'from pynput.mouse import Button', 'from pynput.keyboard import Key' and 'import pynput' – Lukas Sep 09 '18 at 01:59
  • Happy to help! Also, (I don't mean to be rude or pushy, but) please consider [accepting my answer](https://stackoverflow.com/help/someone-answers) :) – Aviv Shai Sep 09 '18 at 03:04
  • Opps sorry, kind new in the stackoverflow community. Sometimes I forget to do those things, I already accepted your answer. – Lukas Sep 09 '18 at 10:52