简体   繁体   中英

ImportError: No module named pytesseract

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).

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!

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 pytesseract tells 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.py as described in this answer .

I had the same error. My solution is

$ pip3 install pytesseract

since I have python 2 and python 3 installed together.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM