Home > Mobile >  ImportError : No Module Named pytesseract
ImportError : No Module Named pytesseract

Time:01-02

I have installed pytesseract successfully but still getting this error in vscode.

i tried installing tesseract in my venv in vscode. and it was successfully installed. but still , I'm getting this error.

i used a simple code i.e.,

    from PIL import Image
import pytesseract as pt
def tesseract():
    path_to_tesseract = r"C:\Program Files\Tesseract-OCR\tesseract.exe"
    image_path="test.jpg"
    pt.tesseract_cmd = path_to_tesseract
    text = pt.image_to_string(Image.open(image_path))
    print(text)
tesseract()

CodePudding user response:

Import errors occur either due to the package not being installed or being installed in a different path.

Since you said you installed pytesseract, I’m guessing it’s the latter. Try running your script in verbose mode with the -v flag to see the path in which Python looks for your packages.

Then you can manually check if pytesseract is installed inside there or somewhere else.

CodePudding user response:

While in your venv, can you try running

pip3 freeze > installed-modules.txt

and share the output here? That should show us what modules are installed in your venv and we can continue from there. Also, do you have the Python Environment Manager extension installed in VS Code?

So sorry but last initial troubleshooting step, in the bottom right corner of VS code, when you have your python file open, does it state that it's using the venv? This can be really telling if its just not utilizing your venv

vs-code demonstration venv

CodePudding user response:

The most common way this ImportError can occur is if pip install refers to a different Python version than VS Code is using, or the virtual environment is not activated in VS Code, or it was not activated when pytesseract was installed.

You can check that pip matches the Python version VS Code is using by running:

pip -V

at the command line, and remember it's output. Then, in VS Code run the following Python script and make sure they match. If they match, then there shouldn't be an issue. If they don't match, some combinations of the virtual environment and the Python Version are not setup correctly.

import sys, subprocess
subprocess.check_call([sys.executable, "-m", "pip", "-V"])

If this is the issue, it can be solved by installed pytesseract by running the following Python script in VS Code.

import sys, subprocess

subprocess.check_call([sys.executable, "-m", "pip", "install", "pytesseract"])

import pytesseract

To explain the command, subprocess.check_call executes a call on the command line, as if you had typed it at the terminal. The difference is uses the full path to the Python Interpreter, from sys.executable (including the virtual environment) to make sure it is setup right.

If this completes without an ImportError, then pytesseract should be working now.

  • Related