简体   繁体   English

ModuleNotFoundError:没有名为“ pynput”的模块

[英]ModuleNotFoundError: No module named 'pynput'

I am writing some basic code in visual studio code and I am trying to use pynput, but when I import the module despite the fact that I installed it using pip it gives me this error: 我正在Visual Studio代码中编写一些基本代码,并且尝试使用pynput,但是尽管我使用pip安装了模块,但尽管导入模块,却出现了以下错误:

ModuleNotFoundError: No module named 'pynput'

I have tried to install it using pip3, but it doesn't work I have also tried to install it using the path interpreter, but it still doesn't work This is the code: 我尝试使用pip3安装它,但是它不起作用我也尝试使用路径解释器安装它,但是仍然不起作用,这是代码:

from pynput.mouse import Button, Controller

mouse = Controller()

# Read pointer position
print('The current pointer position is {0}'.format(
    mouse.position))

Strange thing is that this code works in sublime text 3, but doesn't work in neither visual studio code nor cmd. 奇怪的是,此代码在崇高的文字3中有效,但在Visual Studio代码和cmd中均无效。

Thank you in advance. 先感谢您。

Your package associations may be incorrect. 您的包裹关联可能不正确。

First, see where your IDE is running python. 首先,查看您的IDE在哪里运行python。 it should be something like C:\\programData\\Python 它应该像C:\\ programData \\ Python

Reinstalling the python interpreter may fix this. 重新安装python解释器可能会解决此问题。 Or try upgrading the pip, which uninstalls the old one, and pulls down the new one from the cloud. 或尝试升级pip,该pip会卸载旧的pip,然后从云中拉出新的pip。 Open a CMD windows, and type the following command: 打开CMD窗口,然后键入以下命令:

python -m pip install --upgrade pip --user

This will give you a fresh pip installation. 这将为您提供全新的pip安装。 Then try "pip install pynput" 然后尝试“ pip install pynput”

If this does not solve the issue, uninstall your current interpreter, then go to python.org, and download and install the latest interpreter. 如果这样做不能解决问题,请卸载当前的解释器,然后转到python.org,然后下载并安装最新的解释器。 Upgrade the pip. 升级点子。

if you are trying to run it from within the IDE, check the paths in witch it calls the python interpreter. 如果您试图从IDE中运行它,请检查它调用python解释器的路径。

if it's pointing to any conda installation try conda install pynput instead 如果它指向任何conda安装,请尝试使用conda install pynput代替

Most IDEs create an "interpreter" for your project, which in python-speak means that the IDE set up a "virtual environment" for you. 大多数IDE都会为您的项目创建一个“解释器”,以python方式讲,这意味着IDE会为您设置“虚拟环境”。 Virtual environments are great for managing dependencies across different projects. 虚拟环境非常适合管理不同项目之间的依赖关系。 For example if you need one version of pynput for one project and a later version for another project, you can do this with two separate virtual environments, whereas if you installed pynput on your system, upgrading pynput would break your first project. 例如,如果一个项目需要一个版本的Pynput,而另一个项目需要一个更高的版本,则可以在两个单独的虚拟环境中执行此操作,而如果您在系统上安装了Pynput,则升级Pynput会破坏您的第一个项目。 More info on virtual environments 有关虚拟环境的更多信息

When you open command line and run pip install , this installs the package onto your system interpreter. 当您打开命令行并运行pip install ,这会将软件包安装到系统解释器上。 You'll instead need to 'activate' your virtual environment and run the pip install there. 相反,您需要“激活”您的虚拟环境并在此处运行pip安装。 You can find the path to your virtual environment by opening your interpreter settings in your IDE. 您可以通过在IDE中打开解释器设置来找到虚拟环境的路径。 Then follow these instructions to activate your virtual environment and run the pip install on your project interpreter. 然后按照以下说明激活虚拟环境,并在项目解释器上运行pip install

Try this 尝试这个

pip uninstall pynput
pip install pynput

or 要么

install pynput using conda 使用conda安装pynput

conda install pynput

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

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