添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I'm trying to build a simple keylogger using the pynput library which I've installed using pip, when I write "import pynput" it compiles fine, but once I add a sub library like mouse or keyboard( from pynput.mouse import Controller ) it says that pynput.mouse is not package. I'm a newbie to python and please explain what this error means. Thanks in advance. This is the code:

from pynput.keyboard import Key,Listener
import logging
file="C:\\Users\\lenovo\\Documents\\log.txt"
logging.basicConfig(filename=file,level=logging.DEBUG,format='%(asctime)s: %(message)s')
def on_press(key):
    logging.info(key)
with Listener(on_press=on_press) as listener:
    listener.join()

AND THIS IS THE ERROR:

Traceback (most recent call last):
  File "C:\Users\lenovo\Documents\pynput.py", line 1, in <module>
    from pynput.keyboard import Key
  File "C:\Users\lenovo\Documents\pynput.py", line 1, in <module>
    from pynput.keyboard import Key
ModuleNotFoundError: No module named 'pynput.keyboard'; 'pynput' is not a package

Your code is totally ok but the problem is with your environment. Looks like you don't have pynput package installed in your working python environment. If you are using PyCharm then from the Terminal(located on bottom left) run: pip install pynput
You can also use windows command prompt if you are using windows to run pip install pynput.
To learn more about python virtual env check this link: https://docs.python.org/3/tutorial/venv.html

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.