Python Camelot / Ghostscript“错误的架构”错误

Posted

技术标签:

【中文标题】Python Camelot / Ghostscript“错误的架构”错误【英文标题】:Python Camelot / Ghostscript "wrong architecture" error 【发布时间】:2021-01-21 00:05:09 【问题描述】:

我遇到了一个错误,超出了我的调试能力。 Camelot 对 Ghostscript 的使用似乎发现了错误架构的可执行文件。

采取的步骤:

    brew install Ghostscript 检查是否可以找到 Ghostscript 的可执行文件,as per the docs,但找不到。 将/opt/homebrew/bin/opt/homebrew/lib 添加到适当的路径(我认为?) 根据 Camelot 的 GitHub 上错误 #282 的解决方法,我更改了 camelot/camelot/ext/ghostscript/_gsprint.py Line 256

libgs = ctypes.util.find_library("gs")libgs = distutils.spawn.find_executable("gs")

    使用新的(以上)代码再次检查依赖项。成功。 /opt/homebrew/bin/gs python3.9 -m pip install camelot-py 尝试使用 Ghostscript 运行 Camelot。错误如下所示。

我应该注意,/opt/homebrew/bin/gs 位置中的 gs 似乎是一个别名,不确定这是否重要,但它有理由抛出异常。但是,据我所知,错误中传达的下两个“发现”/opt/homebrew/bin/gs/opt/homebrew/Cellar/ghostscript/9.53.3_1/bin/gs 都是正确的可执行文件。

常识告诉我,如果我使用的是 Windows,我会遇到 64 位与 32 位的问题……但我使用的是我不熟悉的 MacOS。

MacOS -- Apple Silicon M1 -- Python 3.9 -- brew 安装 Ghostscript -- pip 安装 camelot-py -- 通过 Jupiter notebook 尝试

edit -- 还应注意 Python 2.7 是 MacBook 附带的基础安装,建议不要将其删除。 which pythonpython -- version 返回的是 2.7 版本,而不是我运行的 3.9 版本。

---------------------------------------------------------------------------
OSError                                   Traceback (most recent call last)
/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/camelot/ext/ghostscript/_gsprint.py in <module>
    259     try:
--> 260         libgs = cdll.LoadLibrary("libgs.so")
    261     except OSError:

/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/ctypes/__init__.py in LoadLibrary(self, name)
    451     def LoadLibrary(self, name):
--> 452         return self._dlltype(name)
    453 

/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/ctypes/__init__.py in __init__(self, name, mode, handle, use_errno, use_last_error, winmode)
    373         if handle is None:
--> 374             self._handle = _dlopen(self._name, mode)
    375         else:

OSError: dlopen(libgs.so, 6): image not found

During handling of the above exception, another exception occurred:

OSError                                   Traceback (most recent call last)
<ipython-input-4-3a80516ee21f> in <module>
      1 file = r"/Users/joe_kiefner/Documents/Projects/Migration Station/Data_Lake/Arkansas/AR_12_10_2020.pdf"
      2 
----> 3 tables = camelot.read_pdf(file, flavor = 'lattice', pages='5')
      4 
      5 t_df = tables[0].df

/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/camelot/io.py in read_pdf(filepath, pages, password, flavor, suppress_stdout, layout_kwargs, **kwargs)
    111         p = PDFHandler(filepath, pages=pages, password=password)
    112         kwargs = remove_extra(kwargs, flavor=flavor)
--> 113         tables = p.parse(
    114             flavor=flavor,
    115             suppress_stdout=suppress_stdout,

/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/camelot/handlers.py in parse(self, flavor, suppress_stdout, layout_kwargs, **kwargs)
    169             parser = Lattice(**kwargs) if flavor == "lattice" else Stream(**kwargs)
    170             for p in pages:
--> 171                 t = parser.extract_tables(
    172                     p, suppress_stdout=suppress_stdout, layout_kwargs=layout_kwargs
    173                 )

/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/camelot/parsers/lattice.py in extract_tables(self, filename, suppress_stdout, layout_kwargs)
    400             return []
    401 
--> 402         self._generate_image()
    403         self._generate_table_bbox()
    404 

/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/camelot/parsers/lattice.py in _generate_image(self)
    209 
    210     def _generate_image(self):
--> 211         from ..ext.ghostscript import Ghostscript
    212 
    213         self.imagename = "".join([self.rootname, ".png"])

/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/camelot/ext/ghostscript/__init__.py in <module>
     22 #
     23 
---> 24 from . import _gsprint as gs
     25 
     26 

/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/camelot/ext/ghostscript/_gsprint.py in <module>
    274         if not libgs:
    275             raise RuntimeError("Please make sure that Ghostscript is installed")
--> 276         libgs = cdll.LoadLibrary(libgs)
    277 
    278 del __win32_finddll

/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/ctypes/__init__.py in LoadLibrary(self, name)
    450 
    451     def LoadLibrary(self, name):
--> 452         return self._dlltype(name)
    453 
    454     __class_getitem__ = classmethod(_types.GenericAlias)

/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/ctypes/__init__.py in __init__(self, name, mode, handle, use_errno, use_last_error, winmode)
    372 
    373         if handle is None:
--> 374             self._handle = _dlopen(self._name, mode)
    375         else:
    376             self._handle = handle

OSError: dlopen(/opt/homebrew/bin/gs, 6): no suitable image found.  Did find:
    /opt/homebrew/bin/gs: mach-o, but wrong architecture
    /opt/homebrew/Cellar/ghostscript/9.53.3_1/bin/gs: mach-o, but wrong architecture

【问题讨论】:

【参考方案1】:

我遇到了类似的问题,并设法解决了。

我的问题是我使用的是 x86 版本的 Python(通过 Anaconda)。我通过运行发现了这一点:

$ file $(which python)
/Users/cscanlin/opt/anaconda3/envs/marvin/bin/python: Mach-O 64-bit executable x86_64

已通过安装新的通用 Python 二进制文件修复:

$ file /Library/Frameworks/Python.framework/Versions/3.10/bin/python3
/Library/Frameworks/Python.framework/Versions/3.10/bin/python3: Mach-O universal binary with 2 architectures: [x86_64:Mach-O 64-bit executable x86_64] [arm64:Mach-O 64-bit executable arm64]
/Library/Frameworks/Python.framework/Versions/3.10/bin/python3 (for architecture x86_64):   Mach-O 64-bit executable x86_64
/Library/Frameworks/Python.framework/Versions/3.10/bin/python3 (for architecture arm64):    Mach-O 64-bit executable arm64

遗憾的是,还没有官方支持安装基本 Anaconda 的 arm。

【讨论】:

以上是关于Python Camelot / Ghostscript“错误的架构”错误的主要内容,如果未能解决你的问题,请参考以下文章

Python-Camelot:用三行代码提取PDF表格数据

Python 使用camelot将PDF转Excel,简单实用

AttributeError:模块“camelot”没有属性“read_pdf”

Camelot-py 没有检测到有两行的表

Camelot 未在表中检测到表

USACO 3.3 Camelot