Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 654
kilonet Avatar asked Jul 13 '26 19:07

kilonet


2 Answers

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 following these steps, in the same order:

  • brew install ghostscript
  • conda install ghostscript, which installs the arm_64 based library from conda-forge
  • If conda throws a channel error try using conda install -c conda-forge ghostscript
  • pip install ghostscript

Note:

  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
  3. Python doesn't recognise ghostscript module from conda install unless pip install is run, so that is an essential step
like image 160
Prajual Pillai Avatar answered Jul 16 '26 09:07

Prajual Pillai


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.

like image 29
Qasem Nick Avatar answered Jul 16 '26 08:07

Qasem Nick



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!