Python Ghostscript:运行时错误:找不到Ghostscript库(libgs)

7
尝试运行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个回答

8

对于使用 M1 mac 的新用户,ghostscript 可能会显示缺少 libgs 文件的错误,并且文件在 usr/local/lib 不可用。

按照以下步骤依次解决此问题:

  • brew install ghostscript
  • conda install ghostscript,它从 conda-forge 安装了基于 arm_64 的库
  • 如果 conda 抛出频道错误,请尝试使用 conda install -c conda-forge ghostscript
  • pip install ghostscript

注意:

  1. libgs.dylib 文件可以在 ghostscript 的 home brew 安装中找到
  2. 更改 _gsprint.py 中的地址将会抛出错误,因为 home brew 安装版本将是基于 arm_64 的,而 pip 安装版本将是基于 OS_X86 的。
  3. 除非运行 pip install,否则 Python 将无法识别来自 conda install 的 ghostscript 模块,因此这是一个必要的步骤

3

如果您正在使用的是Mac M1,并且使用的是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了。

2
谢谢 - 这在M2芯片组上运行良好(无需使用Conda双重安装ghostscript)。与复制库文件不同,使用符号链接同样有效,并且可以更轻松地进行后续更新。 sudo ln -s /opt/homebrew/Cellar/ghostscript/10.0.0/lib/libgs.dylib /usr/local/lib/libgs.dylib - timhj

2

对我来说,这只是因为我安装了Python的部分:

pip install ghostscript

但是不包括C部分:
brew install ghostscript

也许这些DMG也可以使用,但我没有选择这条路线:http://pages.uoregon.edu/koch/

1
为了解决这个问题,您需要修改site-packages中的ghostscript模块路径。在_gsprint.py中进行修改,修改成类似以下的内容: libgs = ctypes.util.find_library('/opt/local/lib/libgs') 这将定位到您的libgs文件。请注意,不要删除html标签。

1

你应该看一下Ghostscript官方下载网站以及他们的文档

如果这不能帮助你入门,你也可以在IRC服务器irc.freenode.net上名为#ghostscript的在线聊天频道中直接向GS开发人员提问。他们是一群非常友好和乐于助人的人。


0

对于Mac用户,请在/usr/local/lib/目录中查找libgs.x.xx.dylib的版本。

然后运行此命令:

ln -s /usr/local/lib/libgs.x.xx.dylib /usr/local/lib/libgs.so

echo 'export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib/' >> ~/.zshrc

.zshrc 替换为您的 shell 脚本类型。

[将 x.xx 替换为您目录中的版本]


1
Mac用户也可以尝试将以下行 libgs = ctypes.util.find_library("gs")替换为`import distutils.spawn` `libgs = distutils.spawn.find_executable("gs")`链接 - https://github.com/atlanhq/camelot/issues/282#issuecomment-604465648 - Arpan Kushwaha

0

对于那些在 Mac 上尝试了 @PrajualPillai 的答案但仍然无效的其他用户,我将 @ArpanKushwaha 的评论作为答案发布,因为这是唯一对我有效的方法。

由于某种原因,ctypes.util.find_library("gs") 找不到我的 ghostscript 安装位置,所以我不得不进行替换。

/opt/anaconda3/lib/python3.8/site-packages/ghostscript/_gsprint.py

在文件顶部添加:

import distutils.spawn

然后替换

libgs = ctypes.util.find_library('gs')

使用

libgs = distutils.spawn.find_executable("gs")

_gsprint.py文件的底部附近。

如果您还使用camelot进行pdf解析,则还需要在/opt/anaconda3/lib/python3.8/site-packages/camelot/backends/ghostscript_backend.py 文件中将library = find_library("gs")替换为library = distutils.spawn.find_executable("gs")


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接