Day 46 — Oh no! A bug :(

Today I read through some open issues on camelot, and found a bug for when you install it from conda-forge. I'd assumed that installing ghostscript from conda-forge installs all of its dependencies. It does, but looks like all the those depedencies are statically linked into one gs executable.

This would've been fine till camelot ran gs in a subprocess call, but the code was changed to use libgs some time ago. The bug should've been caught when that change was merged, but right now the only test in the conda-forge recipe is to check if camelot can be imported. That didn't catch the bug as the error happens when camelot.read_pdf() is called :(

The fix is to install libgs using the system package manager (apt / brew), or by downloading the setup for Windows from the ghostscript website. Hopefully, a fix for this won't be needed after the default pdf to image conversion backend is switched to pdftopng. But till then I need to update the docs with a note. Huge thanks to Jim Hall for reporting this, and for pointing me in the right direction!

These are the steps I'd used to reproduce the bug initially:

  $ sudo apt remove --auto-remove ghostscript

That removed a ton of packages including ubuntu-gnome-desktop (?!) which I'm supposed to be using! My system still works fine though, need to figure this out later.

After that I create a new conda environment, installed ghostscript from conda-forge, and camelot from PyPI:

  $ conda create --name gs-env python=3.8
  $ conda activate gs-env
  $ conda install -c conda-forge ghostscript
  $ which gs
  $ pip install camelot-py[cv]

And then ran the test Jim described in the issue.

  >>> import camelot
  >>> tables = camelot.read_pdf('foo.pdf')

It worked fine and fed into my confirmation bias, until Jim pointed out that I should check which libgs is being used, and if removing ghostscript removed libgs too or not!

  >>> from ctypes.util import find_library
  >>> find_library("gs")

Indeed! libgs was still present on my system!

  $ whereis /usr/lib/x86_64-linux-gnu/
  $ apt search libgs
  libgs9/focal-updates,focal-security,now 9.50~dfsg-5ubuntu4.2 amd64 [installed]
    interpreter for the PostScript language and for PDF - Library

I didn't remove it though as it was going to take away evince and a lot of other useful packages.

I compared the shared library dependencies of ubuntu and conda-forge ghostscript and found a stark contrast!

  $ ldd /usr/bin/gs (0x00007ffc065d0000) => /usr/lib/x86_64-linux-gnu/ (0x00007efd6bada000) => /lib/x86_64-linux-gnu/ (0x00007efd6b8e8000) => /usr/lib/x86_64-linux-gnu/ (0x00007efd6b867000) => /usr/lib/x86_64-linux-gnu/ (0x00007efd6b7cc000) => /usr/lib/x86_64-linux-gnu/ (0x00007efd6b7c4000) => /usr/lib/x86_64-linux-gnu/ (0x00007efd6b78c000) => /usr/lib/x86_64-linux-gnu/ (0x00007efd6b76d000) => /usr/lib/x86_64-linux-gnu/ (0x00007efd6b6e8000) => /lib/x86_64-linux-gnu/ (0x00007efd6b6cc000) => /usr/lib/x86_64-linux-gnu/ (0x00007efd6b671000) => /lib/x86_64-linux-gnu/ (0x00007efd6b522000) => /lib/x86_64-linux-gnu/ (0x00007efd6b51c000) => /lib/x86_64-linux-gnu/ (0x00007efd6b4e5000) => /usr/lib/x86_64-linux-gnu/ (0x00007efd6b4df000) => /usr/lib/x86_64-linux-gnu/ (0x00007efd6b498000) => /usr/lib/x86_64-linux-gnu/ (0x00007efd6b3d9000) => /usr/lib/x86_64-linux-gnu/ (0x00007efd6b383000) => /lib/x86_64-linux-gnu/ (0x00007efd6b360000)
      /lib64/ (0x00007efd6ca7c000) => /usr/lib/x86_64-linux-gnu/ (0x00007efd6b0f5000) => /usr/lib/x86_64-linux-gnu/ (0x00007efd6b04c000) => /lib/x86_64-linux-gnu/ (0x00007efd6b023000) => /usr/lib/x86_64-linux-gnu/ (0x00007efd6ae15000) => /usr/lib/x86_64-linux-gnu/ (0x00007efd6adc8000) => /usr/lib/x86_64-linux-gnu/ (0x00007efd6adba000) => /usr/lib/x86_64-linux-gnu/ (0x00007efd6ada5000) => /usr/lib/x86_64-linux-gnu/ (0x00007efd6abcf000) => /lib/x86_64-linux-gnu/ (0x00007efd6aba1000) => /lib/x86_64-linux-gnu/ (0x00007efd6ab98000) => /usr/lib/x86_64-linux-gnu/ (0x00007efd6aabb000) => /usr/lib/x86_64-linux-gnu/ (0x00007efd6aa88000) => /lib/x86_64-linux-gnu/ (0x00007efd6aa81000) => /usr/lib/x86_64-linux-gnu/ (0x00007efd6aa72000) => /lib/x86_64-linux-gnu/ (0x00007efd6aa21000) => /usr/lib/x86_64-linux-gnu/ (0x00007efd6a8eb000) => /usr/lib/x86_64-linux-gnu/ (0x00007efd6a8ca000) => /usr/lib/x86_64-linux-gnu/ (0x00007efd6a746000) => /usr/lib/x86_64-linux-gnu/ (0x00007efd6a730000) => /usr/lib/x86_64-linux-gnu/ (0x00007efd6a6f6000) => /usr/lib/x86_64-linux-gnu/ (0x00007efd6a6be000) => /usr/lib/x86_64-linux-gnu/ (0x00007efd6a63a000) => /lib/x86_64-linux-gnu/ (0x00007efd6a633000) => /lib/x86_64-linux-gnu/ (0x00007efd6a615000) => /lib/x86_64-linux-gnu/ (0x00007efd6a568000) => /usr/lib/x86_64-linux-gnu/ (0x00007efd6a55c000) => /lib/x86_64-linux-gnu/ (0x00007efd6a551000) => /usr/lib/x86_64-linux-gnu/ (0x00007efd6a530000) => /usr/lib/x86_64-linux-gnu/ (0x00007efd6a410000) => /lib/x86_64-linux-gnu/ (0x00007efd6a3ed000)

ubuntu ghostscript has so many shared library dependencies, but conda-forge ghostscript does not:

  $ ldd /home/vinayak/anaconda3/envs/gs-3.8/bin/gs (0x00007ffc8dba3000) => /lib/x86_64-linux-gnu/ (0x00007f33bd3e3000) => /lib/x86_64-linux-gnu/ (0x00007f33bd3c0000) => /lib/x86_64-linux-gnu/ (0x00007f33bd1ce000)
      /lib64/ (0x00007f33becb9000)

It's possible that conda-forge ghostscript is one statically linked executable.

There's also a stark difference in the sizes for both executables:

  $ du -sh /usr/bin/gs
  16K /usr/bin/gs
  $ du -sh /home/vinayak/anaconda3/envs/gs-3.8/bin/gs
  25M /home/vinayak/anaconda3/envs/gs-3.8/bin/gs

To reproduce the bug in a clean environment, I launched a docker container with the latest ubuntu image, and installed all the requirements:

  $ docker run -it ubuntu /bin/bash
  $ apt update && apt install curl git
  $ curl -O
  $ bash
  $ eval "$(/root/anaconda3/bin/conda shell.bash hook)"
  (base) $ conda create --name gs-env python=3.8
  (base) $ conda activate gs-env
  (gs-env) $ conda install -c conda-forge camelot-py

Installing camelot from conda-forge installs ghostscript. But I couldn't find libgs!

  (gs-env) python3
  >>> from ctypes.util import find_library
  >>> find_library("gs")
  (gs-env) which gs
  (gs-env) whereis libgs

After that I tried to run the code snippet Jim had posted:

  (gs-env) $ git clone
  (gs-env) $ cd camelot/tests/files
  (gs-env) ./camelot/tests/files $ python3
  >>> import camelot
  Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    File "/root/anaconda3/envs/gs-env/lib/python3.8/site-packages/camelot/", line 6, in <module>
      from .io import read_pdf
    File "/root/anaconda3/envs/gs-env/lib/python3.8/site-packages/camelot/", line 5, in <module>
      from .handlers import PDFHandler
    File "/root/anaconda3/envs/gs-env/lib/python3.8/site-packages/camelot/", line 9, in <module>
      from .parsers import Stream, Lattice
    File "/root/anaconda3/envs/gs-env/lib/python3.8/site-packages/camelot/parsers/", line 4, in <module>
      from .lattice import Lattice
    File "/root/anaconda3/envs/gs-env/lib/python3.8/site-packages/camelot/parsers/", line 26, in <module>
      from ..image_processing import (
    File "/root/anaconda3/envs/gs-env/lib/python3.8/site-packages/camelot/", line 3, in <module>
      import cv2
  ImportError: cannot open shared object file: No such file or directory

But ran into another bug! opencv depends on, which was not already there on this base ubuntu image, and I had to install libgl1-mesa-glx to fix this opencv import error.

  (gs-env) ./camelot/tests/files $ python3
  >>> import camelot
  >>> tables = camelot.read_pdf('foo.pdf')
  Traceback (most recent call last):
    File "/root/anaconda3/envs/gs-env/lib/python3.8/site-packages/camelot/ext/ghostscript/", line 260, in <module>
      libgs = cdll.LoadLibrary("")
    File "/root/anaconda3/envs/gs-env/lib/python3.8/ctypes/", line 451, in LoadLibrary
      return self._dlltype(name)
    File "/root/anaconda3/envs/gs-env/lib/python3.8/ctypes/", line 373, in __init__
      self._handle = _dlopen(self._name, mode)
  OSError: cannot open shared object file: No such file or directory

  During handling of the above exception, another exception occurred:

  Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    File "/root/anaconda3/envs/gs-env/lib/python3.8/site-packages/camelot/", line 113, in read_pdf
      tables = p.parse(
    File "/root/anaconda3/envs/gs-env/lib/python3.8/site-packages/camelot/", line 171, in parse
      t = parser.extract_tables(
    File "/root/anaconda3/envs/gs-env/lib/python3.8/site-packages/camelot/parsers/", line 402, in extract_tables
    File "/root/anaconda3/envs/gs-env/lib/python3.8/site-packages/camelot/parsers/", line 211, in _generate_image
      from ..ext.ghostscript import Ghostscript
    File "/root/anaconda3/envs/gs-env/lib/python3.8/site-packages/camelot/ext/ghostscript/", line 24, in <module>
      from . import _gsprint as gs
    File "/root/anaconda3/envs/gs-env/lib/python3.8/site-packages/camelot/ext/ghostscript/", line 267, in <module>
      raise RuntimeError("Please make sure that Ghostscript is installed")
  RuntimeError: Please make sure that Ghostscript is installed

Finally the bug that I was looking for! Installing libgs9 fixed it, but this is not ideal. I need to come up with a Windows wheel for pdftopng so that I can finally replace ghostscript as the default pdf to image conversion backend in camelot. Is there a way to somehow launch "Windows containers" to debug things?