You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Python的PyWhatKit发送WhatsApp图片报错求助

PyWhatKit发送图片报错问题排查

问题描述

使用PyWhatKit包发送.jpg格式图片时出现报错,但文本消息可正常发送。终端报错信息如下:

(WhatsApp) manunat@Manus-iMac-2 test % python3 send_msg1.py
2024-05-18 08:31:58.137 osascript[1576:27338] CFURLGetFSRef was passed a URL which has no scheme (the URL will not work with other CFURL routines)

按照官方文档编写的代码:

import pywhatkit as pwk

phone_num = "+919xxxxxxxxx"
pwk.sendwhats_image(phone_num, "image.jpg")

环境配置:

  • Python 3.12.3
  • macOS 14.2.1(Sonoma)

问题原因与解决方法

1. 图片路径格式错误

报错核心提示URL缺少协议(scheme),这是PyWhatKit处理相对路径时的常见问题。

  • 解决:改用图片的绝对路径。可以通过代码自动生成绝对路径,或者手动填写完整路径:
    import pywhatkit as pwk
    import os
    
    phone_num = "+919xxxxxxxxx"
    # 自动获取当前目录下图片的绝对路径
    img_abs_path = os.path.abspath("image.jpg")
    pwk.sendwhats_image(phone_num, img_abs_path)
    

2. 版本兼容性问题

Python 3.12属于较新的版本,PyWhatKit可能存在适配漏洞。

  • 解决:
    • 尝试更新PyWhatKit到最新版本:
      pip install --upgrade pywhatkit
      
    • 如果更新后仍报错,可临时降级Python到3.11版本进行测试。

3. macOS自动化权限限制

PyWhatKit依赖AppleScript控制浏览器或WhatsApp客户端,Sonoma系统的隐私设置可能拦截了相关操作。

  • 解决:
    • 打开「系统设置」→「隐私与安全性」→「自动化」,找到Python或终端应用,确保其拥有控制Safari/Chrome/WhatsApp的权限。
    • 确认WhatsApp桌面端已登录并处于运行状态(若使用桌面端发送)。

内容的提问来源于stack exchange,提问作者MJay

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.23 16:46:07