Python<无此文件或目录:'gs'>即使在Macintosh上安装了GhostScript也会出现错误*问题仍然存在!* [英] Python <No such file or directory: 'gs'> error even with GhostScript installed on Macintosh *Issue still Persisting!*

查看:248
本文介绍了Python<无此文件或目录:'gs'>即使在Macintosh上安装了GhostScript也会出现错误*问题仍然存在!*的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在程序中实现了以下保存功能,该功能使用户可以将使用Turtle在Tkinter画布上绘制的任何内容保存为JPEG文件.它应该如何工作是首先捕获屏幕和Tkinter画布,然后基于它创建后记文件.然后,它将后记文件转换为PIL(Python影像库)可读文件类型,然后PIL将转换后的文件另存为JPEG.我的保存功能如下所示:

I have implemented the following save function in my program which allows the user to save as a JPEG file whatever he/she draws on the Tkinter canvas with the Turtle. How it is supposed to work is that it first captures the screen and Tkinter canvas and then creates a postscript file based on it. Then it converts that postscript file as a PIL (Python Imaging Library) readable file type, and then the PIL saves the converted file as a JPEG. My save function is shown below:

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

但是,每当运行此保存功能时,都会显示以下错误:

However, whenever I run this save function, I get the error shown below:

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!之所以知道这一点,是因为我首先通过位于此处的磁盘映像安装了该文件并运行了安装程序.但是,由于此操作不起作用,因此我在 Terminal (终端)中完成了此操作:

even though I already have GhostScript installed! I know this because I first installed it through the Disk Image located here and ran the installer. However, since that did not work, I then did this in Terminal:

`pip install ghostscript`

由于这仍然行不通,因此我在终端机中运行了该程序:

and since even that did not work, I then ran this in Terminal:

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

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

I used 3 ways to install GhostScript and still keep getting the same error. Why is that and how else would I solve this issue?

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

FYI: Just in case you need to know, my operating system is Mac OS 10.11.2 and my Python version is 3.5.1

编辑:因此,正如有一次评论所言,请尝试使用Homebrew解决此问题.因此,自然地我安装了它(因为我还没有安装Homebrew).然后,成功安装后,我运行了brew install ghostscript.解决了一些问题之后,我就能够成功安装GhostScript(无论如何,根据Homebrew的说法).然后我运行brew doctor,发现它没有链接! **** Gas​​p ****好吧,修复很容易,只需将其链接即可!因此,这就是我所做的,并且解决了其他一些问题之后,我终于能够成功执行brew link ghostscript.但是,尽管我进行了英勇的,胆大妄为的尝试来修复该错误,但是它仍然存在……是的,尽管我已尽力而为,但我运行保存功能时IDLE中的错误仍然出现!!此外,运行在终端返回:

So, as a comment once said, try to solve this using Homebrew. So, naturally I installed it (since I did not have Homebrew already installed). Then, upon successful installation, I ran brew install ghostscript. After a few kinks I solved, I was able to successfully install GhostScript (well, according to Homebrew anyways). Then I ran brew doctor and found out it was unlinked! ****Gasp**** Well, a fix is easy enough, just link it! So, that is just what I did, and after fixing a few other kinks, I was finally able to successfully execute brew link ghostscript. However, despite my heroic, bodacious attempts at fixing the error, it remained... Yep, despite my best efforts, the error in IDLE when I run my save function still occurs! Additionally, running gs in Terminal returns:

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

还有什么问题?为什么Python(甚至终端机)找不到gs?

What is STILL going wrong? Why can't Python (or even Terminal) find gs?

编辑#2:好的,所以我终于能够使用Homebrew 安装GhostScript.我要做的就是安装xQuarts,这显然是GhostScript所需要的.但是,我的最初问题仍然没有解决! gs现在可在终端机上运行:

EDIT # 2: Okay, so I have finally been able to install GhostScript using Homebrew. All I had to do was install xQuarts, which GhostScript apparently needs. HOWEVER, my initial issue is STILL not fixed! gs now works in Terminal:

:~ #######$ 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中继续收到此错误:

But my save function STILL does not work in Python, and now I am SURE I have GhostScript installed! I still keep on getting this error in IDLE whenever I run my save function:

FileNotFoundError: [Errno 2] No such file or directory: 'gs'

出了什么问题?

编辑#3: 此处是我的Python脚本,以防万一.

EDIT # 3: Here is my Python script, just in case it is needed.

- 编辑#4已删除 -

-EDIT # 4 Removed-

编辑#5:我有一个小小的预感,这很重要,因此我将其摆在那儿.每当我在Terminal中运行brew doctor时,都会收到并不重要的警告,但是由于某种原因,这一警告对我而言很突出.我真的不知道为什么,但是无论如何,这里是:

EDIT # 5: I have a small hunch this is important, so I am putting it out there. Whenever I run brew doctor in Terminal, I get warnings that are not important, but this one stands out to me for some reason. I really don't know why, but anyways, here it is:

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.

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

Maybe this is what is causing my issues? If so, how would I fix it?

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

EDIT # 6: Apparently it works for others (both windows AND Mac), but still NOT FOR ME! I STILL keep on getting this error whenever I run my program's savefirst() function:

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在终端中起作用并且如此:

It's REALLY weird because gs WORKS in Terminal AND this:

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

在Python IDLE中工作.但是由于某种原因,Python Imaging Library(PIL)仍然不能检测到gs(可能是由于其subprocess.Popen()函数?).这是为什么? 我在Macintosh上都安装了xQuarts,GhostScript和PIL ,但是当我运行savefirst()函数时,我仍然仍然收到错误消息! 为什么?

works in Python IDLE. But for some reason, Python Imaging Library (PIL) still CANNOT detect gs (maybe because of its subprocess.Popen() function?). Why is that? I have xQuarts, GhostScript, and PIL all installed on my Macintosh, but I still get the error when I run my savefirst() function! Why?

P.S.这都是在Macintosh(OS 10.11.2)上发生的,因此最好是熟练使用Macintosh的人回答!

- 编辑#7已删除 -

-EDIT # 7 Removed-

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

EDIT # 8: Okay, I have finally been able to PERMANENTLY (hopefully...) add /usr/local/bin/gs to my PATH (as you can see below):

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然后输入以下内容来添加该路径:

I added that path by running pico ~MyName/.bash_profile and then entering:

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

进入/.bash.profile文件.但是,我仍然继续在Python中获取FileNotFoundError!我是否以错误的方式甚至在错误的目录中将/usr/local/bin/gs附加到了PATH中? 如果不是这种情况,那么为什么Python Imaging Library无法找到gs ??

into the /.bash.profile file. However, I STILL keep getting that FileNotFoundError in Python! Did I append /usr/local/bin/gs to my PATH the wrong way, or even in the wrong directory? If that is not the case, then why can't the Python Imaging Library STILL find gs??

编辑#9:哇!问题解决了!该错误不再出现.我所要做的就是从Mac Path中删除/usr/local/bin/gs,并且显然将其添加到我的PYTHONPATH中,就像在程序开始时一样:

EDIT # 9: Hurrah! Issue RESOLVED! The error does NOT appear anymore. All I had to do was REMOVE /usr/local/bin/gs from my Mac Path, and apparently add it instead to my PYTHONPATH, like so at the beginning of my program:

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

Huzzah!有用! PIL现在可以找到gs,并且可以将画布另存为JPEG,尽管它的分辨率很低,但这是另一个问题.现在,我很高兴解决了最初的问题,终于可以将画布另存为JPEG(感谢@Copperfield!):)

Huzzah! It works! PIL can now find gs, and can save the canvas as JPEG, although it is in a very low resolution, but that is an issue for another question. For now, I am just glad the initial issue is resolved and I can finally save the canvas as a JPEG (Thanks to @Copperfield!) :)

推荐答案

只是一个想法:检入,也许您需要修改一些环境变量,直到该功能生效为止?

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?

编辑

在聊天室中进行交谈,我们发现了问题所在,那就是

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

编辑2

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

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

编辑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

这篇关于Python&lt;无此文件或目录:'gs'&gt;即使在Macintosh上安装了GhostScript也会出现错误*问题仍然存在!*的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆