Python <no such =“”file =“”或=“”目录:=“”'gs'=“”>即使在Macintosh上安装了GhostScript也会出错*问题仍然存在!*

时间:2016-01-02 08:46:32

标签: python macos python-3.x save ghostscript

我在程序中实现了以下保存功能,允许用户将他/她在Tkinter画布上绘制的任何内容保存为JPEG文件。它应该如何工作是它首先捕获屏幕和Tkinter画布,然后基于它创建一个postscript文件。然后它将该postscript文件转换为PIL(Python Imaging Library)可读文件类型,然后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的磁盘映像安装它并运行安装程序。但是,由于这不起作用,我在终端

中执行了此操作
`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,发现它没有关联! ****喘气****嗯,修复很容易,只需链接它!所以,这正是我所做的,在解决了其他一些问题之后,我终于能够成功执行brew link ghostscript。然而,尽管我在修复错误时采取了英勇而艰难的尝试,但它仍然是......尽管我付出了最大努力,但当我运行保存功能时,IDLE中的错误仍然会发生!此外,正在运行{{终止中的1}}返回:

gs

什么是STILL出错了?为什么Python(甚至终端)无法找到dyld: Library not loaded: /opt/X11/lib/libXt.6.dylib Referenced from: //anaconda/bin/gs Reason: image not found Trace/BPT trap: 5

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

gs

但我的保存功能 STILL在Python中不起作用,现在我确定我安装了GhostScript!每当我运行保存功能时,我仍然继续在IDLE中收到此错误:

:~ #######$ 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.

还有什么问题?

编辑#3: Here是我的Python脚本,以防万一需要。

- 编辑#4已移除 -

编辑#5:我有一个小预感,这很重要,所以我把它放在那里。每当我在终端中运行FileNotFoundError: [Errno 2] No such file or directory: 'gs' 时,我都会收到不重要的警告,但出于某种原因,这个警告很突出。我真的不知道为什么,但无论如何,这里是:

brew doctor

也许这就是造成我问题的原因?如果是这样,我该如何解决?

编辑#6:显然它适用于其他人(包括Windows和Mac),但仍然不适合我!每当我运行程序的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. 函数时,我仍会继续收到此错误:

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

适用于Python IDLE。但由于某种原因,Python Imaging Library(PIL)仍然 检测x=subprocess.Popen(['/usr/local/bin/gs', '--version'], stdout=subprocess.PIPE) print(x.stdout.read()) (可能是因为它的subprocess.Popen()函数?)。这是为什么? 我的Macintosh上安装了xQuarts,GhostScript和PIL ,但是当我运行gs功能时,仍然出错! 为什么?

P.S。这一切都发生在Macintosh(OS 10.11.2)上,因此熟悉Macintosh的人的答案是首选!

- 编辑#7已移除 -

编辑#8:好的,我终于能够永久地(希望......)将savefirst()添加到我的路径中(如下所示):

/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 然后输入:

添加了该路径
pico ~MyName/.bash_profile

进入PATH="Library/usr/local/bin/gs:${PATH}" export PATH 文件。但是,我仍然继续在Python中获得/.bash.profile!我是以错误的方式将FileNotFoundError附加到我的PATH,还是在错误的目录中? 如果不是这样,那么为什么Python成像库仍然无法找到/usr/local/bin/gs ??

编辑#9:华友世纪!问题解决了!该错误不再出现。我所要做的就是从我的Mac路径中删除gs,然后显然将它添加到我的PYTHONPATH中,就像在程序开始时一样:

/usr/local/bin/gs

好哇!有用! PIL现在可以找到os.environ["PATH"] += ":/usr/local/bin:/usr/local/bin/gs" ,并且可以将画布保存为JPEG,尽管它的分辨率非常低,但这是另一个问题的问题。现在,我很高兴初始问题得到解决,我终于可以将画布保存为JPEG(感谢@Copperfield!):)

3 个答案:

答案 0 :(得分:1)

只是一个想法:检查PIL的code,它使用shutil模块查找执行gs的{​​{1}}可能您需要修改一些环境变量,直到那工作?

修改

在聊天室里聊天,我们发现似乎是什么问题,subprocess.Popen无法找到shutil.which("gs"),但我们陷入了困境。任何想法??

编辑2

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

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

编辑3

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

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

答案 1 :(得分:0)

使用XQuartz 2.7.8(http://www.xquartz.org)无法在10.11.2上使用自制软件,相同的python版本进行复制。也找到了这个(https://discussions.apple.com/thread/1922974?tstart=0

答案 2 :(得分: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

但默认安装通常是经过最严格测试的。