Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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'

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`

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?

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

EDIT: 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

What is STILL going wrong? Why can't Python (or even Terminal) find 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.

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'

What is still going wrong?

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

-EDIT # 4 Removed-

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?

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'

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

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. This is all occuring on Macintosh (OS 10.11.2), so answers from those proficient with Macintosh are preferred!

-EDIT # 7 Removed-

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

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

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

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

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! 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!) :)

like image 531
R. Kap Avatar asked Jan 02 '16 08:01

R. Kap


People also ask

How do I fix No such file or directory in Python?

To solve No Such File Or Directory Error in Python, ensure that the file exists in your provided path. To check all the files in the directory, use the os. listdir() method.

How do I know if Ghostscript is installed in Python?

In the command line window type “ GSWIN64 -h ” if your system is 64 bit (most machines these days), or “ GSWIN32 -h ” if your system is 32 bit (older machines). If Ghostscript is installed you will see Ghostscript help information.


1 Answers

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
like image 129
Copperfield Avatar answered Nov 14 '22 23:11

Copperfield