小能豆

为什么 pyauto gui 和 Pycharm 对我来说不起作

py

import pyautogui
pyautogui.PAUSE = 1
pyautogui.FAILSAFE = True

pyautogui.click(274, 783)

我在 Mac 上运行 Pycharm。我尝试从解释器和通过 pip3 的终端安装 pyautogui。我尝试运行一个简单的测试,看看它是否会为我执行 1 次鼠标单击,但响应如下:

/Users/user/PycharmProjects/NewContractBuyingMacro/venv/bin/python "/Users/user/PycharmProjects/NewContractBuyingMacro/Buying FD Macro 1.py"
Traceback (most recent call last):
  File "/Users/user/PycharmProjects/NewContractBuyingMacro/Buying FD Macro 1.py", line 3, in <module>
    import pyautogui
  File "/Users/user/PycharmProjects/NewContractBuyingMacro/venv/lib/python2.7/site-packages/pyautogui/__init__.py", line 241, in <module>
    import mouseinfo
  File "/Users/user/PycharmProjects/NewContractBuyingMacro/venv/lib/python2.7/site-packages/mouseinfo/__init__.py", line 100, in <module>
    from rubicon.objc import ObjCClass, CGPoint
  File "/Users/user/PycharmProjects/NewContractBuyingMacro/venv/lib/python2.7/site-packages/rubicon/objc/__init__.py", line 3, in <module>
    from .runtime import (  # noqa: F401
  File "/Users/user/PycharmProjects/NewContractBuyingMacro/venv/lib/python2.7/site-packages/rubicon/objc/runtime.py", line 785
    self.restype, *self.argtypes = ctypes_for_method_encoding(self.encoding)
                  ^
SyntaxError: invalid syntax

我以为这可能与 Mac 不希望程序控制鼠标有关,但现在我不这么认为了。我想这很简单。任何帮助都值得感激。


阅读 17

收藏
2024-12-10

共1个答案

小能豆

The issue you’re encountering is related to the version of Python you’re using. The error message suggests that you’re running Python 2.7 (which is indicated by the path /venv/lib/python2.7/), but the libraries you’re using, such as pyautogui and rubicon, are written for Python 3.

Solution Steps:

  1. Use Python 3:
    First, ensure you’re using Python 3 for your project. Python 2.7 has reached the end of its life and is no longer supported by many modern libraries. You can create a virtual environment with Python 3 in PyCharm.

  2. Create a new virtual environment with Python 3:

  3. In PyCharm, go to Preferences > Project > Project Interpreter.
  4. Click the gear icon and select Add.
  5. Choose Virtualenv and ensure that you select Python 3.x as the interpreter.

  6. Install pyautogui and other dependencies for Python 3:
    Once you have Python 3 set up in your virtual environment, you need to reinstall pyautogui and other libraries:
    bash pip install pyautogui

  7. Check your Python version:
    To verify you’re using Python 3, run:
    bash python --version
    It should show something like Python 3.x.x.

  8. Run the script:
    After switching to Python 3, run your script again. The issue with the SyntaxError should be resolved.

Let me know if this helps or if you encounter further issues!

2024-12-10