Python <No such file or directory: 'gs'> 错误,即使在 Macintosh 上安装了 GhostScript *问题仍然存在!*

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

我在我的程序中实现了以下保存功能,它允许用户将 he/she 在 Tkinter canvas 上使用 Turtle 绘制的任何内容保存为 JPEG 文件。它应该如何工作是它首先捕获屏幕和 Tkinter canvas,然后基于它创建一个 postscript 文件。然后它将 postscript 文件转换为 PIL(Python 成像库)可读文件类型,然后 PIL 将转换后的文件保存为 JPEG。我的保存功能如下图:

def savefirst(): 
    # Capture screen and Tkinter canvas
    cnv = getscreen().getcanvas() 
    global hen
    # Save screen and canvas as Postscript file
    ps = cnv.postscript(colormode = 'color')
    # Open a Tkinter file dialog that allows to input his.her own name for the file
    hen = filedialog.asksaveasfilename(defaultextension = '.jpg')
    # Convert Postscript file to PIL readable format
    im = Image.open(io.BytesIO(ps.encode('utf-8')))
    # Finally save converted file as a JPEG
    im.save(hen + '.jpg')

但是,每当我 运行 这个保存功能时,我都会得到如下所示的错误:

line 2396, in savefirst
    im.save(hen + '.jpg')
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/PIL/Image.py", line 1646, in save
    self.load()
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/PIL/EpsImagePlugin.py", line 337, in load
    self.im = Ghostscript(self.tile, self.size, self.fp, scale)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/PIL/EpsImagePlugin.py", line 143, in Ghostscript
    stdout=subprocess.PIPE)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/subprocess.py", line 950, in __init__
    restore_signals, start_new_session)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/subprocess.py", line 1544, in _execute_child
    raise child_exception_type(errno_num, err_msg)
FileNotFoundError: [Errno 2] No such file or directory: 'gs'

尽管我已经安装了 GhostScript!我知道这一点是因为我首先通过位于 here 和 运行 安装程序的磁盘映像安装了它。但是,由于那不起作用,我随后在 Terminal 中执行了此操作:

`pip install ghostscript`

因为即使那样也不起作用,我然后 运行 在终端中这样做:

`conda install --channel https://conda.anaconda.org/mlgill ghostscript`

我用了 3 种方法 安装 GhostScript,但仍然出现同样的错误。为什么会这样,我还能如何解决这个问题?

仅供参考:以防万一您需要知道,我的操作系统是 Mac OS 10.11.2,我的 Python 版本是 3.5.1

编辑: 所以,正如曾经说过的评论,尝试使用 Homebrew 解决这个问题。所以,我自然而然地安装了它(因为我还没有安装 Homebrew)。然后,安装成功后,我运行brew install ghostscript。在我解决了一些问题之后,我能够成功安装 GhostScript(好吧,无论如何根据 Homebrew)。然后我 运行 brew doctor 发现它未被 link 编辑! ****喘气**** 好吧,修复很容易,只需 link 即可!所以,这就是我所做的,在解决了其他一些问题之后,我终于能够成功执行 brew link ghostscript。然而,尽管我英勇、大胆地尝试修复错误,但它仍然存在......是的,尽管我尽了最大努力,当我 运行 我的保存功能 时 IDLE 中的错误仍然发生! 此外,终端 returns:

中的 运行ning gs
dyld: Library not loaded: /opt/X11/lib/libXt.6.dylib
  Referenced from: //anaconda/bin/gs
  Reason: image not found
Trace/BPT trap: 5

还有什么问题吗?为什么 Python(甚至 Terminal)找不到 gs

编辑 # 2: 好的,所以我终于能够使用 Homebrew 安装 GhostScript 。我所要做的就是安装 GhostScript 显然需要的 xQuarts。但是,我最初的问题仍未解决! gs 现在可以在终端中使用:

:~ #######$ gs
GPL Ghostscript 9.18 (2015-10-05)
Copyright (C) 2015 Artifex Software, Inc.  All rights reserved.
This software comes with NO WARRANTY: see the file PUBLIC for details.

但是我的保存功能在Python中仍然不起作用,现在我确定我已经安装了GhostScript!每当我 运行 我的保存功能:

时,我仍然会在 IDLE 中继续收到此错误
FileNotFoundError: [Errno 2] No such file or directory: 'gs'

还有什么问题吗?

编辑 # 3: Here 是我的 Python 脚本,以备不时之需。

-编辑 # 4 删除-

编辑 # 5: 我有一个小小的预感,这很重要,所以我把它放在那里。每当我在终端中 运行 brew doctor 时,我都会收到不重要的警告,但出于某种原因,这个警告对我来说很突出。我真的不知道为什么,但无论如何,这里是:

Warning: Python is installed at /Library/Frameworks/Python.framework

Homebrew only supports building against the System-provided Python or a
brewed Python. In particular, Pythons installed to /Library can interfere
with other software installs.

也许这就是导致我出现问题的原因?如果是这样,我该如何解决?

编辑 # 6: 显然它适用于其他人(windows 和 Mac),但仍然不适合我!每当我 运行 我程序的 savefirst() 函数时,我仍然会继续收到此错误:

line 2395, in savefirst
    im.save(hen + '.jpg')
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/PIL/Image.py", line 1646, in save
    self.load()
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/PIL/EpsImagePlugin.py", line 337, in load
    self.im = Ghostscript(self.tile, self.size, self.fp, scale)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/PIL/EpsImagePlugin.py", line 143, in Ghostscript
    stdout=subprocess.PIPE)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/subprocess.py", line 950, in __init__
    restore_signals, start_new_session)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/subprocess.py", line 1544, in _execute_child
    raise child_exception_type(errno_num, err_msg)
FileNotFoundError: [Errno 2] No such file or directory: 'gs'

这真的很奇怪,因为 gs 在终端中工作并且:

x=subprocess.Popen(['/usr/local/bin/gs', '--version'], stdout=subprocess.PIPE)
print(x.stdout.read())

在 Python 空闲状态下工作。但出于某种原因,Python 成像库 (PIL) 仍然 CANNOT 检测到 gs(可能是因为它的 subprocess.Popen() 函数?)。这是为什么? 我的 Macintosh 上安装了 xQuarts、GhostScript 和 PIL,但是当我 运行 我的 savefirst()函数! 为什么?

P.S。这一切都发生在 Macintosh (OS 10.11.2) 上,因此优先考虑那些精通 Macintosh 的人的回答!

-编辑 # 7 删除-

EDIT # 8: 好的,我终于能够永久地(希望...)将 /usr/local/bin/gs 添加到我的路径中(如下所示) :

Library/usr/local/bin/gs:/Library/Frameworks/Python.framework/Versions/3.5/bin:/Library/Frameworks/Python.framework/Versions/3.5/bin:/Library/Frameworks/Python.framework/Versions/3.5/bin:/Library/Frameworks/Python.framework/Versions/3.5/bin://anaconda/bin:/Users/Rohan/anaconda/bin:/Library/Frameworks/Python.framework/Versions/3.5/bin:/Library/Frameworks/Python.framework/Versions/3.4/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin

我通过 运行ning pico ~MyName/.bash_profile 添加了该路径,然后输入:

PATH="Library/usr/local/bin/gs:${PATH}"
export PATH

进入/.bash.profile文件。但是,我仍然在 Python 中继续得到 FileNotFoundError!我是否以错误的方式将 /usr/local/bin/gs 附加到我的 PATH,甚至是在错误的目录中? 如果不是这样,那么 为什么 Python 成像库仍然找不到 gs??

编辑# 9:万岁!问题解决了!该错误不再出现。我所要做的就是从我的 Mac 路径中删除 /usr/local/bin/gs,显然将它添加到我的 PYTHONPATH 中,就像在我的程序开始时那样:

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

万岁!有用! PIL 现在可以找到 gs,并且可以将 canvas 保存为 JPEG,虽然它的分辨率很低,但这是另一个问题的问题。现在,我很高兴最初的问题得到解决,我终于可以将 canvas 保存为 JPEG(感谢@Copperfield!):)

无法使用自制软件在 10.11.2 上复制,相同的 python 版本,使用 XQuartz 2.7.8 (http://www.xquartz.org). Found this too (https://discussions.apple.com/thread/1922974?tstart=0)

您似乎缺少依赖项。下载 homebrew 并从中安装 ghostscript:

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

...

brew install ghostscript

也有一些选项可以使用brew info ghostscript获得:

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

但默认安装通常是 most-tested。

只是一个想法:检查 code of PIL, it use the shutil 模块以发现 gs 正在做 shutil.which("gs") 也许您需要修改一些环境变量直到它起作用?

编辑

在聊天室聊天,我们发现了问题所在,那就是 subprocess.Popen 找不到 gs,但我们卡在那里了。有什么想法吗??

编辑 2

我找到了一个可行的解决方案,首先我从我的路径中删除 gs,然后尝试这个

>>> 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'
>>> 

为了使此功能与您的代码一起工作,我进行了以下更改:

#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

并且测试它工作正常。

在你的情况下可能是:

#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

是否不起作用尝试其中之一

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"]

编辑 3

随着这个解决方案的工作,一个更自动化的版本可以是这样的:

#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