Search code examples
pythonscipyosx-elcapitanimshow

scipy imshow conflict with El Capitan SIP and /var/folders/


I run into some permission issues and I am sure there is a quick fix, but I can't find it.

Setup: Mac, OSX: El Capitan, Anaconda, Python3.5.1

From bash (make Preview the default viewer):

export SCIPY_PIL_IMAGE_VIEWER=/Applications/Preview.app/Contents/MacOS/Preview

From interpreter:

import scipy.misc as mi

im1 = mi.imread('local_filename.png')
mi.imshow(im1)

Spits the following error from Preview: The file “tmph_9q7lwu.png” couldn’t be opened because you don’t have permission to view it.

The path of this file is in: /var/folders/w4/wrnzszgd41d7064lx64nc10h0000gn/T/

Its permissions are: -rw------, Preview app's UID: 501 (myself)

I feel that this is an El Capitan SIP (system integrity protection) issue, which leads me to my question:

  1. is there a way to make scipy write temporary files to a folder accessible by Preview,
  2. or, is there a way to give Preview permission to read out of /var/folder/ without disabling SIP.

Thanks!


Solution

  • I found a little hack that works, there are two steps:

    Within pilutil.py, editing lines 381+ (under the def imshow),

    import os
    import time #Me: 1. needed later
    os.close(fnum)
    
    cmd = os.environ.get('SCIPY_PIL_IMAGE_VIEWER', 'see') 
    cmd = 'open'  #Me: 2. User added line
    print(fname)
    status = os.system("%s %s" % (cmd, fname))
    
    time.sleep(0.1) #Me: 1.    
    os.unlink(fname)
    

    Step 1. Preview needs a time delay (0.05 was too fast). Otherwise the unlink deletes the file before Preview displays it correctly.

    Step 2. Using open instead of the $SCIPY_PIL_IMAGE_VIEWER reference to the Preview app works. The latter method has a more general error.

    There is a larger problem though, which I might raise in a separate post. From within bash:

    /Applications/Preview.app/Contents/MacOS/Preview /path/to/file
    

    This should cause Preview to open the file. But instead it raises the permissions error, seemingly regardless of where the file is located.