python ghostscript: RuntimeError: 找不到Ghostscript库(libgs)

7 投票
7 回答
14580 浏览
提问于 2025-04-16 22:22

当我尝试运行 hello-world 示例时

import sys
import ghostscript

args = [
    "ps2pdf", # actual value doesn't matter
    "-dNOPAUSE", "-dBATCH", "-dSAFER",
    "-sDEVICE=pdfwrite",
    "-sOutputFile=" + sys.argv[1],
    "-c", ".setpdfwrite",
    "-f",  sys.argv[2]
    ]

ghostscript.Ghostscript(*args)

出现了错误:

 File "/Users/ddd/sss/ddd/eee.py", line 2, in <module>
    import ghostscript
  File "build/bdist.macosx-10.6-universal/egg/ghostscript/__init__.py", line 33, in <module>

  File "build/bdist.macosx-10.6-universal/egg/ghostscript/_gsprint.py", line 290, in <module>
RuntimeError: Can not find Ghostscript library (libgs)

这个 libgs 库是什么?我该怎么获取它呢?

顺便说一下,我是在 Mac 上操作的。

7 个回答

2

对我来说,问题很简单,就是我安装了Python的部分:

pip install ghostscript

但没有安装C语言的部分:

brew install ghostscript

也许这些DMG文件也可以用,但我没有尝试这个方法:

http://pages.uoregon.edu/koch/

4

好的,如果你在使用M1芯片的Mac,并且用的是python 3.9,那么之前提到的GitHub问题里的方法可能不太管用。我试过几次@Prajual建议的方法,但也没成功。这个方法有帮助。

  1. 首先,
brew install ghostscript
  1. 在brew库里检查一下你的版本。应该会有一个类似9.56.1_1的目录。
ls /opt/homebrew/Cellar/ghostscript/
  1. 然后把那个dylib文件复制到pip可以访问的地方,也就是/usr/local/lib/,这需要管理员权限。x.xx.xx就是你上一个命令得到的版本号!
sudo cp /opt/homebrew/Cellar/ghostscript/x.xx.xx/lib/libgs.dylib /usr/local/lib/

现在你应该可以顺利地使用import ghostscript了。

9

对于使用M1 Mac的新用户来说,ghostscript可能会出现缺少libgs文件的错误,而这个文件在usr/local/lib目录下是找不到的。

解决这个问题可以按照以下步骤进行,记得按顺序来:

  • brew install ghostscript:先用这个命令安装ghostscript。
  • conda install ghostscript:这个命令会从conda-forge安装适用于arm_64的库。
  • 如果conda出现频道错误,可以试试用conda install -c conda-forge ghostscript
  • pip install ghostscript:最后用这个命令安装ghostscript。

注意:

  1. libgs.dylib文件可以在通过homebrew安装的ghostscript中找到。
  2. 修改_gsprint.py中的地址会出错,因为通过brew安装的版本是arm_64的,而通过pip安装的版本是OS_X86的。
  3. Python不会识别通过conda安装的ghostscript模块,除非你先运行pip安装,所以这一步是必须的。

撰写回答