MacOS下ModuleNotFoundError: No module named 'pytesseract'求助
解决MacOS下Pytesseract模块找不到的问题(ModuleNotFoundError)
针对你遇到的ModuleNotFoundError: No module named 'pytesseract'问题,结合MacOS 13.4.1和Python3.9的环境,试试以下步骤:
确认pytesseract安装目标环境
MacOS默认自带Python2,容易和Python3.9混淆。先找到Python3.9的可执行文件路径:which python3.9用这个路径对应的pip重新安装pytesseract,确保安装到Python3.9环境:
# 替换成上面输出的路径,示例如下 /Library/Frameworks/Python.framework/Versions/3.9/bin/pip3 install pytesseract安装完成后,用同样路径的pip检查是否安装成功:
/Library/Frameworks/Python.framework/Versions/3.9/bin/pip3 list | grep pytesseract验证脚本运行的Python版本
在你的脚本开头添加一行代码,查看实际运行脚本的Python路径:import sys; print(sys.executable) try: from PIL import Image except ImportError: import Image import pytesseract print(pytesseract.image_to_string(Image.open('/users/lucas/desktop/image.png')))运行脚本后,如果输出的路径和你安装pytesseract的Python3.9路径不一致,说明你用了其他Python环境运行脚本。此时直接用Python3.9的完整路径运行脚本:
# 替换成你的Python3.9路径和脚本路径 /Library/Frameworks/Python.framework/Versions/3.9/bin/python3.9 /users/lucas/desktop/你的脚本文件名.py排查虚拟环境干扰
如果你之前使用过Python虚拟环境,可能pytesseract被安装在虚拟环境内,但你在全局环境运行脚本。此时要么激活对应的虚拟环境再运行脚本,要么在全局的Python3.9环境重新安装pytesseract。提前配置Tesseract路径(避免后续报错)
即使模块问题解决,可能还会遇到Tesseract可执行文件找不到的问题,可在脚本中指定Tesseract路径(先用which tesseract查看实际路径):try: from PIL import Image except ImportError: import Image import pytesseract pytesseract.pytesseract.tesseract_cmd = '/usr/local/bin/tesseract' # 替换为你的实际路径 print(pytesseract.image_to_string(Image.open('/users/lucas/desktop/image.png')))
内容的提问来源于stack exchange,提问作者Lucas Roblin
相关产品推荐
相关产品推荐

