Hi folks, I'm currently working on a feature to pe...
# general
s
Hi folks, I'm currently working on a feature to perform OCR on PDF files that contain embedded images. We convert the PDF to an image using the
pdf2image
library and then run OCR on the image with
pytesseract
. While writing a unit test for this flow, I encountered an issue when running the test with Pants. Pants is unable to open the temporary image we get when converting a pdf -> image, and I’m seeing the error attached in the thread. Interestingly, the test runs fine with Poetry. I suspect this might be related to how Pants sandboxes tests, potentially restricting access to certain temp files generated during execution. Has anyone else run into this issue or have any suggestions on how to resolve it? Thanks!
Copy code
pytesseract.pytesseract.TesseractError: (1, 'Error in fopenReadStream: failed to open locally with tail tess_c67xdrva_input.PPM for filename /tmp/tess_c67xdrva_input.PPM Leptonica Error in findFileFormat: image file not found: /tmp/tess_c67xdrva_input.PPM Error in fopenReadStream: failed to open locally with tail P6 for filename P6 Leptonica Error in pixRead: image file not found: P6 Image file P6 cannot be read! Error during processing.')
b
Sorry for the trouble. I'm not sure about the specifics, but, in general, the best way to debug this sort of thing is to jump into the sandbox and execute code there: https://www.pantsbuild.org/stable/docs/using-pants/troubleshooting-common-issues#debug-tip-inspect-the-sandbox-with---keep-sandboxes In particular if you pass
--keep-sandboxes=on_failure
you'll see the failing test's sandbox printed, and then you can go to that directory and run the
__run.sh
... hopefully that reproduces the problem! Once you've reproduced it outside of a
pants ...
invocation, you can then debug it like "normal" e.g.
pytest --pdb ..
or whatever you need to do
s
Hmm, i think the issue happening is that whenever I convert the pdf to an image using the
convert_from_path
function in
pdf2image
, it temporarily stores the image in a /tmp folder on my computer. Unfortunately since pants creates a sandbox, it probably doesn't have access to this created /tmp folder as it doesn't exist in the sandbox..
b
That doesn’t sound quite right. The sandbox isn’t very strict, and absolute paths like
/tmp
should work fine (we use them in my work repo).