Python <No such file or directory: 'gs'> error even with GhostScript installed on Macintosh *Issue still Persisting!*

别说谁变了你拦得住时间么 提交于 2019-12-04 10:20:54

just a idea: checking in the code of PIL, it use the shutil module to find gs doing shutil.which("gs") maybe you need to modify some environment variable until until that work?

EDIT

talking in the chat room, we find what appear to be the problem, and that is subprocess.Popen can't find gs, but we got stuck there. Any ideas??

EDIT 2

I found a posible solution, first I remove gs from my path, and I try this

>>> import os,shutil,subprocess
>>> shutil.which("gswin64c") # shutil.which("gs")
>>> test = subprocess.Popen(["gswin64c","--version"],stdout=subprocess.PIPE) # subprocess.Popen(["gs","--version"],stdout=subprocess.PIPE)
Traceback (most recent call last): 
  File "<pyshell#3>", line 1, in <module>
    test = subprocess.Popen(["gswin64c","--version"],stdout=subprocess.PIPE)
  File "C:\Anaconda3\lib\subprocess.py", line 950, in __init__
    restore_signals, start_new_session)
  File "C:\Anaconda3\lib\subprocess.py", line 1220, in _execute_child
    startupinfo)
FileNotFoundError: [WinError 2] El sistema no puede encontrar el archivo especificado
>>> os.environ["PATH"]
'C:\\Anaconda3\\Library\\bin;C:\\Anaconda3\\Library\\bin;C:\\Anaconda3;C:\\Anaconda3\\Scripts;C:\\Anaconda3\\Library\\bin;C:\\ProgramData\\Oracle\\Java\\javapath;C:\\Program Files (x86)\\Haskell\\bin;C:\\Program Files (x86)\\Haskell Platform\\2013.2.0.0\\lib\\extralibs\\bin;C:\\Program Files (x86)\\Haskell Platform\\2013.2.0.0\\bin;C:\\Windows\\system32;C:\\Windows;C:\\Windows\\System32\\Wbem;C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\;C:\\Program Files (x86)\\nodejs\\;C:\\Program Files\\MATLAB\\R2009a\\bin;C:\\Program Files\\MATLAB\\R2009a\\bin\\win64;C:\\Program Files (x86)\\Haskell Platform\\2013.2.0.0\\mingw\\bin;C:\\Program Files\\MiKTeX 2.9\\miktex\\bin\\x64\\;C:\\Program Files (x86)\\Skype\\Phone\\;C:\\Anaconda3;C:\\Anaconda3\\Scripts;C:\\Anaconda3\\Library\\bin;C:\\Users\\David\\AppData\\Roaming\\cabal\\bin;C:\\Users\\David\\AppData\\Roaming\\npm;C:\\Program Files (x86)\\Java\\jre7\\bin;C:\\Users\\David\\Dropbox\\Programas Random'
>>> os.environ["PATH"] += ";C:\\Program Files\\gs\\gs9.18\\bin"
>>> shutil.which("gswin64c")
'C:\\Program Files\\gs\\gs9.18\\bin\\gswin64c.EXE'
>>> test = subprocess.Popen(["gswin64c","--version"],stdout=subprocess.PIPE)
>>> test.stdout.read()
b'9.18\n'
>>> 

to make this work with your code the following changes I made:

#this is very first import
import os
print("## Addind gs to environ ##", os.environ["PATH"] )
os.environ["PATH"] += ";C:\\Program Files\\gs\\gs9.18\\bin"
print("## Addind gs to environ ##", os.environ["PATH"] )

#then the others import, and everything else

and testing it work fine.

In your case that maybe is:

#this is very first import
import os
print("## Addind gs to environ ##", os.environ["PATH"] )
os.environ["PATH"] += ":/usr/local/bin" 
print("## Addind gs to environ ##", os.environ["PATH"] )
#then the others import, and everything else

is that don't work try with one of this

os.environ["PATH"] += "Library/usr/local/bin" 
os.environ["PATH"] = "/usr/local/bin:" + os.environ["PATH"]
os.environ["PATH"] = "Library/usr/local/bin:" + os.environ["PATH"]

EDIT 3

as this solution work, a more automatic version of this can be something like this:

#this is the very first thing to do
import os, subprocess, shutil
#see is gs is avaible
if shutil.which("gs") is None:
   print("GhostScrip is not avaible, search for it")
   try:
      gs = subprocess.Popen(["which","gs"],stdout=subprocess.PIPE)
      gs_path = gs.stdout.read()
      gs_path = gs_path.decode() if isinstance(gs_path,bytes) else gs_path
      print("GhostScrip found in",gs_path)
      os.environ["PATH"] += ":"+ os.path.dirname(gs_path)
   except Exception as e:
      raise Warning("GhostScrip not found, this program may fail")

del subprocess
del shutil

#then everything else

Could not replicate on 10.11.2 with homebrew, same python version, using XQuartz 2.7.8 (http://www.xquartz.org). Found this too (https://discussions.apple.com/thread/1922974?tstart=0)

Looks like you're missing a dependency. Download homebrew and install ghostscript from that:

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

...

brew install ghostscript

There are also options for it which may be obtained using brew info ghostscript:

--with-djvu
        Build drivers for DjVU file format
--with-x11
        Build with x11 support
--HEAD
        Install HEAD version

But the default install is often the most-tested.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!