简体   繁体   中英

python ghostscript: RuntimeError: Can not find Ghostscript library (libgs)

When trying to run hello-world example

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)

getting error:

 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)

what is this libgs library and how can I get it?

btw I'm on mac

For me it was simply that I had installed the python parts:

pip install ghostscript

But not the C parts:

brew install ghostscript

Perhaps these DMGs work also - but I didn't go this route: http://pages.uoregon.edu/koch/

To solve it, you need to modify ghostscript modules path in site-packages.

in _gsprint.py modify into something like this:

libgs = ctypes.util.find_library('/opt/local/lib/libgs')

which locates your libgs file.

You should have a look at Ghostscript's official download site as well as at their documentation .

If this doesn't get you started you could also ask the GS developers directly in their online chat channel named #ghostscript on IRC server irc.freenode.net . They are a very friendly and helpful bunch of people.

For newer user who are using M1 mac, ghostscript might show a missing libgs file error and the file would be unavailable at usr/local/lib

The issue can be resolved by:

pip install ghostscript

Followed by conda install ghostscript , which installs the arm_64 based library from conda-forge

PS:

  1. The libgs.dylib file can be found in the home brew installation of ghostscript
  2. Changing the address in _gsprint.py will throw an error as the brew installed version will be arm_64 based and the pip installed version will be OS_X86 based

Ok, if you're on mac M1 and using python 3.9 that trick from the mentioned Github issue won't work probably. I did a few times what @Prajual suggested but didn't work either. This helps.

  1. First,
brew install ghostscript
  1. Check out in brew lib what your version is. there should be a directory named like 9.56.1_1
ls /opt/homebrew/Cellar/ghostscript/
  1. Then copy that dylib file to where pip can access at /usr/local/lib/ -- needs sudo privilege. x.xx.xx would be your version from last command!
sudo cp /opt/homebrew/Cellar/ghostscript/x.xx.xx/lib/libgs.dylib /usr/local/lib/

You should now be able to import ghostscript with no problem.

For Mac user find the version of the libgs.x.xx.dylib in your /usr/local/lib/ directory.

Then run this command,

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

Replace .zshrc with your shell script type.

[replace x.xx with the version in your directory]

For other users on Mac that have tried @PrajualPillai's answer and it still didn't work, I'm posting @ArpanKushwaha's comment as an answer, because it was the only thing that worked for me.

For some reason, ctypes.util.find_library("gs") could not find my ghostscript install, so I had to replace it in

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

At the top of the file, add

import distutils.spawn

and then replace

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

with

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

near the bottom of _gsprint.py .

If you also use camelot for pdf parsing you also need to replace library = find_library("gs") with library = distutils.spawn.find_executable("gs") in your /opt/anaconda3/lib/python3.8/site-packages/camelot/backends/ghostscript_backend.py file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM