简体   繁体   English

Python 找不到已安装的模块

[英]Installed modules not found by Python

I need to use the autogui module to do something in Python. When I run the script, it says that it can't find the autogui module so I installed it with pip install autogui .我需要使用autogui模块在 Python 中做一些事情。当我运行脚本时,它说找不到autogui模块,所以我用pip install autogui安装了它。 But when I run the script again it still says me this module doesn't exist.但是当我再次运行脚本时,它仍然告诉我这个模块不存在。

Try pyautogui - I had the same problem.尝试pyautogui - 我遇到了同样的问题。 Instead of autogui , write `pyautogui.而不是autogui ,写 `pyautogui. Or, if you are running python3 or higher then try:或者,如果您运行的是 python3 或更高版本,请尝试:

pip3 install pyautogui.

Method 1:方法一:

You're probably having trouble setting up your correct Python Interpreter and working within it,try the following in VSCode.您可能无法设置正确的 Python 解释器并在其中工作,请在 VSCode 中尝试以下操作。

Ctrl + Shift + p Ctrl + Shift + p

And enter the following in the field.并在字段中输入以下内容。

python: select interpreter python: select 口译员

Select the desired environment and reinstall PyAutoGui Select 需要的环境并重新安装PyAutoGui

Method 2:方法二:

Creating a virtual environment for your project where all your packages will be installed and will be isolated from others, and will have no import errors since it's an environment specifically for the project that you're working on.为您的项目创建一个虚拟环境,您的所有包都将在其中安装并与其他包隔离,并且不会出现导入错误,因为它是专门针对您正在处理的项目的环境。

I assume you use Windows, so open the command line in your working directory, or open your working directory in VSCode and enter the following in the command-line tool that is provided within VSCode.我假设你使用 Windows,所以在你的工作目录中打开命令行,或者在 VSCode 中打开你的工作目录并在 VSCode 提供的命令行工具中输入以下内容。

The Python installers for Windows include pip. You should be able to access pip using: Windows 的 Python 安装程序包括 pip。您应该能够使用以下方式访问 pip:

py -m pip --version py -m pip --version

You can make sure that pip is up-to-date by running the following您可以通过运行以下命令确保 pip 是最新的

py -m pip install --upgrade pip py -m pip 安装--升级 pip

Installing virtual environment安装虚拟环境

py -m pip install --user virtualenv py -m pip 安装 --user virtualenv

Creating a virtual environment创建虚拟环境

py -m venv env py -m venv 环境

The second argument is the location to create the virtual environment.第二个参数是创建虚拟环境的位置。 Generally, you can just create this in your project and call it env.通常,您可以在您的项目中创建它并将其命名为 env。

venv will create a virtual Python installation in the env folder. venv 将在 env 文件夹中创建一个虚拟 Python 安装。

Finally, to activate the environment run the following command最后,要激活环境,请运行以下命令

.\env\Scripts\activate .\env\Scripts\activate

That will activate your environment.那会激活你的环境。

pip install pyautogui pip 安装 pyautogui

Make sure to change your interpreter to the one that you just created in the env/bin folder and run your code, or you could just enter the path to the * python file located in the env/bin folder.确保将您的解释器更改为您刚刚在env/bin文件夹中创建的解释器并运行您的代码,或者您可以只输入位于env/bin文件夹中的 * python文件的路径。

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

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