7

I was following this guide https://realpython.com/blog/python/setting-up-a-simple-ocr-server/ and got to the part where I run cli.py python flask_server/cli.py but I get

python cli.py
Traceback (most recent call last):
  File "cli.py", line 3, in <module>
    import pytesseract
ImportError: No module named pytesseract

How can I solve this ?

I also saw that I have multiple versions of python. I have linux-kali installed with the latest updates.

Something else: he runs the command like python flask_server/cli.py- where is that flask_server located ? I simply ran it like python cli.py(I was in some directory in which I created the file).

PeeHaa
  • 71,436
  • 58
  • 190
  • 262
Bogdan Daniel
  • 2,689
  • 11
  • 43
  • 76
  • You must install [pytesseract](https://pypi.python.org/pypi/pytesseract/0.1). It's not part of the standard library. – Two-Bit Alchemist Oct 28 '15 at 21:32
  • I followed the tutorial up to that point so it should be installed – Bogdan Daniel Oct 28 '15 at 21:37
  • Did you run the line `pip install -r requirements.txt` ? This would install all the required packages from the `requirement.txt` file, as explained in [this question](http://stackoverflow.com/questions/8726207/what-are-the-python-equivalents-to-rubys-bundler-perls-carton). – Bertrand Caron Oct 28 '15 at 21:40
  • You mentioned you have multiple versions of Python so I strongly suspect that your `cli.py` and your `pytesseract` are installed relative to two separate versions. Solution: install `pytesseract` under the version you're running your script with, or run script with version that has `pytesseract`. – Two-Bit Alchemist Oct 28 '15 at 21:57
  • I took a script to show me the installed module on the python that was running cli.py and I got back only ['xsser==1.6'] – Bogdan Daniel Oct 28 '15 at 22:04
  • This worked `pip install pytesseract` – Bogdan Daniel Oct 28 '15 at 22:11

4 Answers4

17

Python import errors usually boils down to one of those three cases (whether is is modules you developed; or modules distributed as packages):

  1. You did no install the required package. Googling pytesseracttells me its an OCR that is distributed and installable using Python package manager tool pip by running pip install pytesseract in your favorite shell.

  2. You did install the package, but is is not in your python path.

  3. (Less often) You did install the package, and it is in your python path, but you used a name already in user by Python, and the two are conflicting.

In your case, I strongly think this is the first one. Case 2. and 3. can be assessed by calling python -v your_script.pyas described in this answer.

Community
  • 1
  • 1
Bertrand Caron
  • 2,525
  • 2
  • 22
  • 49
  • I took a script to show me the installed module on the python that was running `cli.py` and I got back only ['xsser==1.6'] – Bogdan Daniel Oct 28 '15 at 22:04
13

I had a similar error. So I hope to help people with this kind of problems.

In my case, I tried to run python code using pytesseract lib on Raspberry pi 3.

$ pip install pillow
$ pip install pytesseract

(followed by https://www.pyimagesearch.com/2017/07/10/using-tesseract-ocr-python/)


and then, I made an example.py to test.

example.py

try:

    import Image

except ImportError:

    from PIL import Image

from pytesseract import *

print(pytesseract.image_to_string(Image.open('YOUR_IMAGE_PATH')))

and then, when I run this code, I got below error like you. ImportError: No module named pytesseract


After I saw the @Bertrand Caron's answer, I found a solution. My problem was package library path.

I also have multiple versions of python, 2.7 and 3.5, like a writer. When I run command $python --version on linux, the result is Python 2.7.13.

In my case, when I installed a pytesseract package, it was stored in "/usr/local/lib/python3.5/dist-packages/pytesseract".

And When I ran $python -v example.py, I found the referenced packages path hadn't been same with upper pytesseract package directory.

cf.

installed pytesseract path : /usr/local/lib/python3.5/dist-packages/pytesseract

actual referenced lib path, when running : /usr/lib/python2.7/dist-packages/

So, I copied pytesseract, located in "/usr/local/lib/python3.5/dist-packages/pytesseract" to "/usr/lib/python2.7/dist-packages/"

Then, Solved!

SeungSoo Kim
  • 131
  • 1
  • 5
5

In my case, I was running it in Jupyter so I used this command,

! pip install --user pytesseract

But I forgot to restart the kernal. You need to restart the kernal after installing the pakcage

Ateik
  • 2,458
  • 4
  • 39
  • 59
3

I had the same error. My solution is

$ pip3 install pytesseract

since I have python 2 and python 3 installed together.

jhnd74
  • 67
  • 6