简体   繁体   English

无法在我的 IDE 中成功导入/使用 pyautogui 库

[英]unable to successfully import/ use pyautogui library in my IDE

I am attempting to use a small script to move my mouse slightly every minute or two to make the appearance that I am sitting at my desk (dont tell my boss please)我正在尝试使用一个小脚本每隔一两分钟稍微移动一次鼠标,以使我看起来像是坐在办公桌前(请不要告诉我的老板)

is anyone able to assist in why this might be happening?有没有人能够帮助解决为什么会发生这种情况?

from my command prompt I was able to successfully install pip install pyautogui从我的命令提示符我能够成功安装pip install pyautogui

note, that: pip3 install autogui was not recognized请注意: pip3 install autogui未被识别

once I opened up pycharm, I ran the code, and got the below error:打开 pycharm 后,我运行代码,得到以下错误:

ModuleNotFoundError: No module named 'pyautogui'

  • I also checked to make sure I had python 3 in command line using pip version = anaconda3\lib\site-packages\pip (python 3.7)我还检查以确保我在命令行中有 python 3 使用 pip version = anaconda3\lib\site-packages\pip (python 3.7)

**full code below for reference **下面的完整代码供参考

import pyautogui
import time
import sys
from datetime import datetime
pyautogui.FAILSAFE = False
numMin = None
if ((len(sys.argv)<2) or sys.argv[1].isalpha() or int(sys.argv[1])<1):
    numMin = 1
else:
    numMin = int(sys.argv[1])
while(True):
    x=0
    while(x<numMin):
        time.sleep(60)
        x+=1
    for i in range(0,200):
        pyautogui.moveTo(0,i*4)
    pyautogui.moveTo(1,1)
    for i in range(0,3):
        pyautogui.press("shift")
    print("Movement made at {}".format(datetime.now().time()))

https://github.com/Johnson468/Stay-Awake https://github.com/Johnson468/Stay-Awake

for reference - i am using windows供参考 - 我正在使用 windows

You cannot import pyautogui from python 3 interpreter in pycharm because it is not installed.您无法从 pycharm 中的 python 3 解释器导入pyautogui ,因为它没有安装。
When you ran pip install pyautogui you installed it on your python2.7 environment.当您运行pip install pyautogui时,您将它安装在您的 python2.7 环境中。

When you tried to installed it with pip3 you ran anaconda's pip (accordint to you error).当您尝试使用pip3安装它时,您运行了 anaconda 的 pip (根据您的错误)。
you can check which pip is used inside the CMD with the command where pip3 .您可以使用where pip3

If it is not found you can try running it like this: python -m pip but make sure you are running python3!如果找不到,您可以尝试像这样运行它: python -m pip但请确保您正在运行 python3!

I highly recommend installing everything in venv virtual environment to make sure nothing is missing and you don't have any dependencies issues.我强烈建议在venv虚拟环境中安装所有内容,以确保没有遗漏任何内容并且您没有任何依赖关系问题。
Or if you are new to python, uninstall all different instances and only keep one (probably python3 in your case), or edit you PATH in Windows或者,如果您是 python 的新手,请卸载所有不同的实例并只保留一个(在您的情况下可能是 python3),或者在 Windows 中编辑您的PATH

Furthermore I'm pretty sure when you are running pip from your console you are using another instance of it on your computer then the one you set as interpreter on pycharm, hence you couldn't use pip3 at all.此外,我很确定当您从控制台运行pip时,您正在计算机上使用它的另一个实例,然后是您在 pycharm 上设置为解释器的实例,因此您根本无法使用 pip3。

If none of the above helped, please add more details yo your question and I'll edit the answer.如果以上都没有帮助,请在您的问题中添加更多详细信息,我将编辑答案。

Summary : You have a few instances of python installed on your computer (at least one of python2.7 and one of anaconda), when you run python or pip from the CMD if refers to the python2.7 one. Summary : You have a few instances of python installed on your computer (at least one of python2.7 and one of anaconda), when you run python or pip from the CMD if refers to the python2.7 one.
But when you chose an interpreter for you project in Pycharm you chose a python3 instance, which is obviously doesn't contain pyautogui .但是,当您在 Pycharm 中为您的项目选择解释器时,您选择了一个 python3 实例,它显然不包含pyautogui
Make sure you are using the right python instance when setting it up in Pycharm and when installing packages.在 Pycharm 中设置和安装软件包时,请确保使用正确的 python 实例。

I answered this question recently: Problem with Python modules import on Mac我最近回答了这个问题: Mac 上的 Python 模块导入问题

The issue might be that you don't have pip installed.问题可能是您没有安装 pip。

I know most Mac's have pip and Python pre-installed, but it wouldn't hurt to try: sudo easy_install pip我知道大多数 Mac 都预装了 pip 和 Python,但尝试一下也无妨: sudo easy_install pip

If this doesn't seem to be the issue, you might be running the pip install packageName in the wrong directory.如果这似乎不是问题,您可能在错误的目录中运行pip install packageName

You will need to access your scripts folder in python and run the pip install packageName command in there.您将需要访问 python 中的脚本文件夹并在其中运行pip install packageName命令。 Here is a resource which should help you reach that directory: https://datatofish.com/install-package-python-using-pip/这是一个可以帮助您到达该目录的资源: https://datatofish.com/install-package-python-using-pip/

This was answered for a Mac, but switching the commands to another OS shouldn't be hard.这在 Mac 上得到了解答,但是将命令切换到另一个操作系统应该不难。 Just make sure pip is installed and that you're installing in the right directory.只需确保已安装 pip 并且您正在安装在正确的目录中。

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

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