安装pytesseract后运行代码遇ModuleNotFoundError问题求助
解决ModuleNotFoundError: No module named 'pytesseract'问题
安装pytesseract模块后运行以下代码时,出现ModuleNotFoundError: No module named 'pytesseract'错误:
from PIL import Image from pytesseract import pytesseract # Define path to tessaract.exe path_to_tesseract = r'C:\Python310\Scripts\pytesseract.exe' # Define path to image path_to_image = 'extract.png' # Point tessaract_cmd to tessaract.exe pytesseract.tesseract_cmd = path_to_tesseract # Open image with PIL img = Image.open(path_to_image) # Extract text from image text = pytesseract.image_to_string(img) print(text)
解决步骤
- 确保使用正确的安装命令:执行
pip install pytesseract完成Python封装库的安装,注意pytesseract是Python模块,和底层的Tesseract OCR引擎是两个独立的东西,引擎需要单独安装,但当前报错为模块未找到,优先解决模块安装问题。 - 匹配Python环境:如果系统存在多个Python版本,确认安装模块的
pip和运行代码的Python是同一个环境:- 若运行代码用
python script.py,则对应使用pip install pytesseract安装 - 若用
python3 script.py,则对应使用pip3 install pytesseract安装 - 虚拟环境需先激活,再执行
pip install pytesseract
- 若运行代码用
- 验证安装状态:在命令行执行
pip show pytesseract,若能输出模块版本、安装路径等信息,说明安装成功;若提示找不到模块,重新执行安装命令,排查是否有网络或权限类报错。 - 检查IDE解释器配置:如果用IDE(如VS Code、PyCharm)运行代码,需确认IDE选择的Python解释器是已经安装了pytesseract的那个环境。
内容的提问来源于stack exchange,提问作者Amira Mohamed Abdelsalam
相关产品推荐
相关产品推荐

