You have pyocr, Pillow, OpenCV and Tesseract installed, the notebook is written, and the first run dies with ModuleNotFoundError: No module named 'tesseract_ocr'. Every install command you try afterwards changes nothing, because the traceback hides two separate faults: a line that imports your own function back as if it were a package, and a wrapper whose view of the Tesseract executable and its language data has not been checked yet. This page walks the failure in the order it bites: read the traceback, delete the self-import, point pyocr at tesseract.exe, install the language your lang argument names, and put the image where the notebook actually looks for it. The traceback, code and outputs match a JupyterLab 4.0.8 set-up running tesseract 4.1.1 from conda, so you can check every step against your own screen.
Symptom: the traceback names a function you wrote
The failure is precise. Cell In[17] dies at line 41, on this line:
from tesseract_ocr import render_doc_text
with ModuleNotFoundError: No module named 'tesseract_ocr'. That error has one meaning: Python searched every directory on sys.path for an importable module with that name and found none. The importable names of the OCR wrapper you installed are pyocr and pyocr.builders — there is no tesseract_ocr module in it, and a search of the conda packages in this set-up found no folder of that name either. The string is not a misremembered package name: it is the function defined in the same cell, render_doc_text, dressed up as a module.
That layout comes from tutorials that keep the function in a file named tesseract_ocr.py and import it into a second script. Paste both halves into one notebook and the import has nothing left to find — which is also why conda install and pip install attempts could never fix it. The missing 'module' was never a package.
The test takes ten seconds: search the notebook for def render_doc_text. If it sits above the failing line, the fix is to delete the import and call the function directly. The next section works the arithmetic on the exact lines.
Worked example: cell In[17], line 41
Inputs (all from the failing notebook): the traceback names cell In[17] and points at line 41; line 39 is return data_list, the last line of render_doc_text; the installed set is pillow, opencv-python, numpy, pyocr and conda's tesseract package, whose folder is tesseract-4.1.1-h8c00e3c_4; a search for any folder named tesseract_ocr returned nothing.
Rule: an import resolves only names that exist as modules on sys.path. A function defined in the same notebook is reached by calling its name — never by importing it.
Arithmetic: 41 − 39 = 2, so the import sits two lines below the end of the very function it tries to import, in the same cell. Modules named tesseract_ocr installed: 0. Definitions of render_doc_text in the cell: 1. A name that exists as a function but never as a module can satisfy a call and can never satisfy an import.
Result: delete line 41 — the traceback's arrow marks the only line that has to go — and keep the call under it, data_list = render_doc_text('sample.png'), which runs the function you already have.
Decision: whenever a ModuleNotFoundError name matches a def in the same file or notebook, delete the import. No install command can create that module; installs fix errors about packages that genuinely exist, such as pyocr or opencv.
Cause 2: pyocr cannot see the tesseract executable
pyocr ships no OCR engine. It is a wrapper that drives the Tesseract executable, so its first job — get_available_tools() — is to find that executable. On Windows, tesseract.exe must either be on PATH or be named through pyocr's own setting, pyocr.tesseract.TESSERACT_CMD. The notebook here already does this, in the only order that works: set the path first, then ask for the tools.
import pyocr
pyocr.tesseract.TESSERACT_CMD = 'C:/Users/your-account/AppData/Local/Programs/Tesseract-OCR/tesseract.exe'
tools = pyocr.get_available_tools() # list of tools; at least 1 entry when the path is right
Two checks tell you whether this half is healthy. Print len(tools): one or more entries means pyocr found the executable; an empty list means the path is wrong, and the next line, tool = tools[0], dies with IndexError: list index out of range. Run !tesseract --list-langs in a cell: if that prints a language list, the executable itself works and the problem is only the path pyocr is given. In this set-up that command succeeded, so the executable was never the failing part — the import above it was.
The conda copy of tesseract lives inside the environment folder, tesseract-4.1.1-h8c00e3c_4 here, not in a global Windows location, which is why a hunt through system folders finds nothing. Point TESSERACT_CMD at the copy you want, or activate the environment so PATH picks it up.
Cause 3: lang='jpn' with only eng installed
The diagnostic you already ran settles this half before the first read: !tesseract --list-langs printed exactly two entries, eng and osd. The code then asks for Japanese: tool.image_to_string(img, lang='jpn', builder=pyocr.builders.TextBuilder()). A language that is not in that list has no data behind it, so the call fails with a Tesseract error about the missing language — after the import fix, this is the next failure waiting in line.
Fix it at the source. The list belongs to whichever tesseract.exe the shell and pyocr resolve to, and in this set-up both point at the installer's copy under AppData/Local/Programs, whose tessdata subfolder the Windows installer created. Either re-run that installer and tick Japanese, or copy the official jpn.traineddata file from the Tesseract project's tessdata repository into that folder. Then confirm: !tesseract --list-langs must list three entries instead of two — eng, osd, jpn — before image_to_string will accept lang='jpn'. Each copy of tesseract on the machine keeps its own tessdata, so after switching TESSERACT_CMD, run the list check again.
If the data is installed and Tesseract still reports it missing, the path — not the file — is at fault: OCR language data not found: set the tessdata path correctly covers that case step by step.
Where sample.png must sit
A relative path like 'sample.png' resolves against the notebook's working directory — the folder the notebook server was started from, which is also where ! shell commands run. One cell settles it:
import os
print(os.getcwd()) # str: the directory relative paths resolve from
Move the image into that directory, or pass an absolute path such as 'C:/data/sample.png'. The failure is silent at the read itself: cv2.imread returns None when it cannot open a file, and the crash only surfaces in the next line, when cv2.threshold receives None. Check immediately:
img = cv2.imread(file_path, 0) # numpy array, or None when the path is wrong
assert img is not None, 'image not found: ' + file_path
When the picture holds more than the text you want — borders, neighbouring labels, fixtures — crop the text region before reading. Detection to OCR pipeline: crop detected regions then read text shows the pattern.
Prevention: five checks before the next run
-
No self-imports. Define the function in one cell and call it in the next; never import a name that a
defin the same notebook already provides. -
One path cell. Set
pyocr.tesseract.TESSERACT_CMDonce, at the top, before anyget_available_tools()call — forward slashes are fine. -
Assert the tool. Right after getting the list, require
len(tools) >= 1and printTESSERACT_CMDin the error message, so an empty list explains itself. -
Check the language. Run
!tesseract --list-langsbefore the first read; the string you pass aslangmust appear in that output. -
Resolve the image path. Anchor relative paths at
os.getcwd()or pass absolute paths, and testimread's result before processing.
Each check converts a crash you discover at run time into a message you wrote yourself.
When the read belongs on the production line
A notebook answers one question — can this text be read from this image? A line asks another: will the read run unattended, part after part, without anyone keeping a Python environment, a PATH entry and a tessdata folder alive on the line PC? When the job is reading printed strings or codes at a station, the sturdier answer is to move the read onto the device: fixed code readers decode on board and hand the string to your PLC or PC over Ethernet. Readers in this catalog read text as well as codes — OCR on a fixed code reader: multiple text strings in one image shows several strings decoded from a single frame.
For small print or dense pages, give the characters more pixels: the MV-ID5120RM-08M-RBN reads through a 12 MP sensor over GigE.
- Interface
- GigE
- Resolution
- 12 MP
- Shutter
- Rolling
- Lens mount
- M12
Tighter stations fit a compact reader instead: the MV-ID3013PM-12M-WBN carries a 12 mm lens on a 1.3 MP sensor and speaks Fast Ethernet through an M12 connector.
- Interface
- Fast Ethernet
- Resolution
- Up to 3 MP
- Shutter
- Global
- Lens mount
- M12
When the station must also judge the part — presence, orientation, print quality — beyond reading it, put the inspection on a smart camera. The MV-SC5060XM-08M-WBN pairs a 6 MP mono sensor with an 8 mm lens, GigE, rolling shutter and an M12 mount.
- Interface
- GigE
- Resolution
- 6-10 MP
- Sensor format
- 1/2.4"
- Shutter
- Rolling
Two closing checks keep the read honest on the line. If characters drop out, the usual cause is too few pixels per character: Minimum font size for OCR: why small text fails and how to fix it gives the sizing rule. And once the string arrives, compare it with the expected value before the part moves on — OCR verify mode: checking printed text against an expected string shows the acceptance check.
The decisions, one line each: delete the self-import; set TESSERACT_CMD before get_available_tools() and require at least one tool; install the language data into the tessdata folder of the executable you actually call and confirm it in --list-langs; anchor the image at os.getcwd() or use its absolute path; and if the read is heading for a line, run it on a reader or camera instead of a notebook. To pick hardware for your station, start at the camera finder or the system configurator, or contact the applications team with the part, the text height and the cycle time.
Questions
Why does pyocr's get_available_tools() return an empty list on Windows?
pyocr is only a wrapper, so an empty list means it did not find tesseract.exe. The path in pyocr.tesseract.TESSERACT_CMD is wrong, or the executable is not on PATH and no path was set. Print the length of the list, correct the path — forward slashes work — and set it before get_available_tools() runs; then tools[0] is safe.
Where do I put jpn.traineddata for Tesseract on Windows?
In the tessdata folder of the installation your TESSERACT_CMD actually selects — not in every copy on the machine. In the set-up here that is the Tesseract-OCR folder under AppData/Local/Programs. Copy the official traineddata file from the Tesseract project there, then run tesseract --list-langs and confirm jpn appears before the first Japanese read.
Do I need if __name__ == '__main__' in a Jupyter notebook?
It is harmless but does nothing: __name__ is already '__main__' inside notebook cells, so guarded code runs anyway. Keep the guard for scripts you run from a command line. In a notebook, define the function in one cell and call it in the next, with no import in between.
Which folder does JupyterLab use for relative image paths?
The folder the notebook server was launched from, shown as the root of the file browser after a default start. Print it with os.getcwd() in a cell; shell commands with ! use the same directory. Keep your images there, or give imread an absolute path so the read no longer depends on where the server was started.
Can a fixed code reader replace this notebook on the line?
Yes, when the task is reading codes or printed strings at a station. A fixed code reader runs decode and OCR on the device and returns the string over Ethernet, so the line PC keeps no Python environment, no PATH entry and no tessdata folder. The notebook stays useful upstream, for proving a difficult part can be read at all.


