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 不希望程序控制鼠标有关,但现在我不这么认为了。我想这很简单。任何帮助都值得感激。
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.
/venv/lib/python2.7/
pyautogui
rubicon
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.
Create a new virtual environment with Python 3:
Choose Virtualenv and ensure that you select Python 3.x as the interpreter.
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
bash pip install pyautogui
Check your Python version: To verify you’re using Python 3, run: bash python --version It should show something like Python 3.x.x.
bash python --version
Python 3.x.x
Run the script: After switching to Python 3, run your script again. The issue with the SyntaxError should be resolved.
SyntaxError
Let me know if this helps or if you encounter further issues!