92

I am trying to use pytesseract in Python but I always end up with the following error:

    raise TesseractNotFoundError()
pytesseract.pytesseract.TesseractNotFoundError: tesseract is not installed or it's not in your path

However, pytesseract and Tesseract are installed on my system.

Example code that produces this error:

import cv2
import pytesseract

img = cv2.imread('1d.png')
print(pytesseract.image_to_string(img))

How do I resolve this TesseractNotFoundError?

S.S. Anne
  • 15,171
  • 8
  • 38
  • 76
PreetyP
  • 931
  • 1
  • 7
  • 4

26 Answers26

112

I got this error because I installed pytesseract with pip but forget to install the binary.

On Linux

sudo apt update
sudo apt install tesseract-ocr
sudo apt install libtesseract-dev

On Mac

brew install tesseract

On Windows

download binary from https://github.com/UB-Mannheim/tesseract/wiki. then add pytesseract.pytesseract.tesseract_cmd = 'C:\\Program Files (x86)\\Tesseract-OCR\\tesseract.exe' to your script. (replace path of tesseract binary if necessary)

references: https://pypi.org/project/pytesseract/ (INSTALLATION section) and https://github.com/tesseract-ocr/tesseract/wiki#installation

Ali
  • 6,808
  • 3
  • 37
  • 47
53

I tried adding to the path variable like others have mentioned, but still received the same error. what worked was adding this to my script:

pytesseract.pytesseract.tesseract_cmd = r"C:\Program Files (x86)\Tesseract-OCR\tesseract.exe"

Sociopath
  • 13,068
  • 19
  • 47
  • 75
  • May be you have installed 64 bit version, Please check the below path if you have 'C:\\Program Files\\Tesseract-OCR\\tesseract.exe'. In case tesseract is available, change that line as below pytesseract.pytesseract.tesseract_cmd = 'C:\\Program Files\\Tesseract-OCR\\tesseract.exe' – Rajesh_Saladi Nov 16 '19 at 07:53
20

You are probably missing tesseract-ocr from your machine. Check the installation instructions here: https://github.com/tesseract-ocr/tesseract/wiki

On a Mac, you can just install using homebrew:

brew install tesseract

It should run fine after that

divibisan
  • 11,659
  • 11
  • 40
  • 58
KornholioBeavis
  • 2,402
  • 2
  • 19
  • 26
16

Under Windows 10 OS environment, the following method works for me:

  1. https://github.com/tesseract-ocr/tesseract/wiki Download tesseract and install it. Windows version is available here: https://github.com/UB-Mannheim/tesseract/wiki

  2. Find script file pytesseract.py from C:\Users\User\Anaconda3\Lib\site-packages\pytesseract and open it. Change the following code from tesseract_cmd = 'tesseract' to: tesseract_cmd = 'D:/Program Files (x86)/Tesseract-OCR/tesseract.exe'

  3. You may also need add environment variable D:/Program Files (x86)/Tesseract-OCR/

Hope it works for you!

ah bon
  • 9,293
  • 12
  • 65
  • 148
13

I'm running on a Mac OS and installed tesseract with brew so here's my take on this. Since pytesseract is just how you can access tesseract from python, you have to specify where tesseract is already on your computer.

For Mac OS

Try finding where the tesseract.exe is- if you installed it using brew, on your the terminal use:

>brew list tesseract

This should list where your tesseract.exe is, somewhere more or less like

> /usr/local/Cellar/tesseract/3.05.02/bin/tesseract

Then following their instructions:

pytesseract.pytesseract.tesseract_cmd = r'<full_path_to_your_tesseract_executable>'

pytesseract.pytesseract.tesseract_cmd = r'/usr/local/Cellar/tesseract/3.05.02/bin/tesseract'

should do the trick!

Louis Lin
  • 161
  • 4
11

For Mac:

  1. Install Pytesseract (pip install pytesseract should work)
  2. Install Tesseract but only with homebrew, pip installation somehow doesn't work. (brew install tesseract)
  3. Get the path of brew installation of Tesseract on your device (brew list tesseract)
  4. Add the path into your code, not in sys path. The path is to be added along with code, using pytesseract.pytesseract.tesseract_cmd = '<path received in step 3>' - (e.g. pytesseract.pytesseract.tesseract_cmd = '/usr/local/Cellar/tesseract/4.0.0_1/bin/tesseract')

This should work fine.

Mihir Verma
  • 290
  • 4
  • 11
11

CAUTION: ONLY FOR WINDOWS


I came across this problem today and all the answers mentioned here helped me, but I personally had to dig a lot to solve it. So let me help all others by putting out the solution to it in a very simple form:

  1. Download the executable 64 bit (32-bit if your computer is of 32 bit) exe from here.

    (Name of the file would be tesseract-ocr-w64-setup-v5.0.0.20190526 (alpha))

  1. Install it. Let it install itself in the default C directory.

  2. Now go to your Environment variable (Reach there by just searching it in the start menu or Go to Control Panel > System > Advanced System Settings > Environment Variables)

a) Select PATH and then Edit it. Click on NEW and add the path where it is installed (Usually C:\Program Files\Tesseract-OCR\)

Now you will not get the error!

  • The "here" link that you have provided is broken, it is missing an "i" after "wik". I found the correct link after going to the module's website. Also, what should be the "Variable Name" when trying to add tesseract to the environment variables, i.e. PATH. – tanmay_garg Sep 04 '20 at 08:27
  • 1
    @tanmay_garg Thanks for pointing out the broken link. The variable name should be 'Path'. Let me explain you better: When you go to Environment Variables, under System Variables you will find a variable named 'Path'. Double click on it and then click on New (once inside the Path), and then add the location path. – Jaydeep Dholakia Sep 05 '20 at 11:19
  • Ohh, I thought you meant that a new user variable had to be created! Got it thanks! Finally understood what adding a package to the "PATH" means ooff :) – tanmay_garg Sep 05 '20 at 15:11
  • @tanmay_garg Happy that I was able to help – Jaydeep Dholakia Sep 07 '20 at 03:46
  • Very instructive and the most clear answer to the problem for Windows users. – Joy Jul 03 '23 at 15:11
10

One simple thing that actually worked for me in Jupyter Notebook, was using double backslash instead of a single backslash in the pytesseract.pytesseract.tesseract_cmd path:

pytesseract.pytesseract.tesseract_cmd = 'C:\\Program Files (x86)\\Tesseract-OCR\\tesseract.exe'
evanca
  • 509
  • 6
  • 9
7

I faced the same problem. I hope you have installed from here and have also done pip install pytesseract.

If everything is fine you should see that the path C:\Program Files (x86)\Tesseract-OCR where tesseract.exe is available.

Adding Path variable did not helped me, I actually added new variable with name tesseract in environment variables with a value of C:\Program Files (x86)\Tesseract-OCR\tesseract.exe.

Typing tesseract in the command line should now work as expected by giving you usage informations. You can now use pytesseract as such (don't forget to restart your python kernel before running this!):

import pytesseract
from PIL import Image

value=Image.open("text_image.png")
text = pytesseract.image_to_string(value, config='')    
print("text present in images:",text)

enjoy!

Nino Filiu
  • 16,660
  • 11
  • 54
  • 84
aniket
  • 101
  • 4
  • Since the os is not specified in the question for Windows Users this will be the approved answer – Pardhu Mar 21 '19 at 05:57
7

I face this same issue. I just use this command that will help me.

sudo apt install tesseract-ocr

Note that this will only work on Ubuntu.
sudo is a Unix exclusive command (Linux, Mac, Rasbian, etc.) while apt is Ubuntu specific.

Fachi
  • 7
  • 2
Vivekanand Panda
  • 832
  • 12
  • 23
6

I was also facing the same error when I was trying to make a text-extractor using pytesseract, but the solution was there in installation instructions for pytesseract in pypi site: pytesseract There are many alternatives to avoid the error, But, adding one more parameter in the method pytesseract.image_to_string solved it for me, like

tessdata_dir_config = "/usr/share/tesseract-ocr/4.00/tessdata"
output = pytesseract.image_to_string(image, lang='eng', config=tessdata_dir_config)
  • Thank you! Only this helped me. I use: pytesseract, jupyter notebook, conda env with only ipykernel installed (jupyter not installed in this env), ubuntu. – OlegK Jul 23 '22 at 14:41
4

The following three commands will do the needful :

sudo apt update
# This will update your packages
sudo apt install tesseract-ocr
# This will install OCR
sudo apt install libtesseract-dev
# This will add it as development dependency
Shivam Bharadwaj
  • 1,864
  • 21
  • 23
3

You can download tesseract-ocr setup using the following link,

Tesseract for windows

Then add new variable with name tesseract in environment variables with value C:\Program Files (x86)\Tesseract-OCR\tesseract.exe

Codemaker2015
  • 12,190
  • 6
  • 97
  • 81
2

Install tesseract from https://github.com/UB-Mannheim/tesseract/wiki and add the path of tesseract.exe to the Path environment variable.

Ranjan Kumar
  • 123
  • 4
2

Most likely you have different versions of Python installed, ensure that the installed Tesseract is on the same Python version.

which pip3 shows you the path to the pip3 installation and which python3 shows the corresponding path to the Python installation.

Ensure that these two are the same.

Andrew Ravus
  • 451
  • 1
  • 7
  • 14
2

This occurs under windows (at least in tesseract version 3.05) when the current directory is on a different drive from where tesseract is installed.

Something in tesseract is expecting data files to be in \Program Files... (rather than C:\Program Files, say). So if you're not on the same drive letter as tesseract, it will fail. It would be great if we could work around it by temporarily changing drives (under windows only) to the tesseract installation drive before executing tesseract, and changing back after. Example in your case: You can copy yourmodule_python.py to "C/Program Files (x86)/Tesseract-OCR/" and RUN!

Sang9xpro
  • 435
  • 5
  • 8
2

There Are few steps to set the path

1:goto this "https://github.com/UB-Mannheim/tesseract/wiki"

2:download the latest installers

3:install it

4: set the path in system variables such as "C:\Program Files\Tesseract-OCR" or "C:\ProgramFiles (x86)\Tesseract-OCR"

5 : open CMD type "tesseract" and some output except "not regonized type errors"

Suraj Verma
  • 463
  • 6
  • 8
2

On Ubuntu under flask webframework this should be working

pytesseract.pytesseract.tesseract_cmd = r"/usr/bin/tesseract"
img = Image.open(picture_name)
print(pytesseract.image_to_string(img))
Prajwol KC
  • 398
  • 6
  • 13
0

Are you importing

from tesseract import image_to_string

Don't import from pytesseract

Zamar
  • 499
  • 6
  • 13
  • I used this statement but got an error "ImportError: cannot import name image_to_string". I tried solving this error but still struggling with it – PreetyP Jun 03 '18 at 16:02
  • I have the same problem as OP. If from tesseract import image_to_string, then: Traceback (most recent call last): File "C:\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 2862, in run_code exec(code_obj, self.user_global_ns, self.user_ns) File "", line 3, in from tesseract import image_to_string File "C:\Anaconda3\lib\site-packages\tesseract\__init__.py", line 34 print 'Creating user config file: {}'.format(_config_file_usr) ^ SyntaxError: invalid syntax – Alexander Chervov Jun 03 '18 at 20:26
  • Are you sure this is correct? If so, can you add a link to the Python module you are referring to? It seems incorrect, since [pytesseract](https://pypi.org/project/pytesseract/) is an optical character recognition (OCR) tool, while [tesseract](https://pypi.org/project/tesseract/) is for "computing concentrations of simulated dark matter halos from volume info for particles generated using Voronoi tesselation". That tesseract library only runs on Python 2 and does not include an `image_to_string` method. – wovano Aug 22 '19 at 05:46
0

I was also facing the same issue, just add C:\Program Files (x86)\Tesseract-OCR to your path variable. If it still does not work, add C:\Program Files (x86)\Tesseract-OCR\tessdata to your path variable in a new line. And do not forget to restart your computer after adding the path variable.

Lovepreet Singh
  • 4,792
  • 1
  • 18
  • 36
emon
  • 1
0

I'm currently using Windows and needed to develop a PDF parser but adding a new environment variable via sysdm.cpl alone did not work. For other Windows user, I strongly suggest adding C:\Program Files (x86)\Tesseract-OCR to your profile.ps1 as well (if using Powershell that is).

noBee
  • 61
  • 6
0

Small mistake -- I knew I had to open/close my cmd to get the updated path to reflect. Using Jupyter Notebook I had to shutdown the client and re-initialize it also.

Schalton
  • 2,867
  • 2
  • 32
  • 44
0

Just run these command if you are using linux,

sudo apt update
sudo apt install tesseract-ocr
sudo apt install libtesseract-dev

then run this,

python -m pip install tesseract tesseract-ocr pytesseract
Jaied
  • 900
  • 9
  • 13
0

for me it worked by putting single quote

pytesseract.pytesseract.tesseract_cmd =r'C:/Program Files/Tesseract-OCR/tesseract.exe'

actually putting inside double quotes was automatically inserting unwanted chracter

Raj
  • 173
  • 5
0

This is my path

pytesseract.pytesseract.tesseract_cmd = r"C:\Program Files\Tesseract-OCR\tesseract.exe"  # your path may be different
Sai_Varma
  • 9
  • 3
0

I changed my path from:

path_to_tesseract = 'C:\Program Files\Tesseract-OCR\tesseract.exe'
to 
path_to_tesseract = r'C:/Program Files/Tesseract-OCR/tesseract.exe' 

issue resolved for me

Varun Singh
  • 63
  • 1
  • 3
  • 13