Answer a question

I am using pynput.mouse.Controller to listen for certain mouse actions and also using it to navigate to certain targets.

When I import Controller as follows: from pynput.mouse import Controller everything works fine and the programs runs smoothly.

However, when I do this instead import pynput.mouse.Controller I receive an error telling ModuleNotFoundError: No module named 'pynput.mouse.Controller'

Unless I am having a fundemental misunderstanding, these lines should function the same. Is there any reason why one produces an error but the other doesn't?

Answers

import imports modules or packages (directories with __init__.py), it cannot import objects from modules. This doesn't work:

import pynput.mouse.Controller

This work:

import pynput.mouse
Controller = pynput.mouse.Controller

This also work:

from pynput input mouse
Controller = mouse.Controller

And this:

from pynput.mouse import Controller
Logo

开发云社区提供前沿行业资讯和优质的学习知识,同时提供优质稳定、价格优惠的云主机、数据库、网络、云储存等云服务产品

更多推荐