Understanding routing error (Python)
Question: I'm trying to make a program work that calls files in a specific folder.
However, for some reason, I keep getting an error. I'll post the relevant code
and error message.

Code:

    
    
    def objmask(inimgs, inwhts, thresh1='20.0', thresh2='2.0', tfdel=True, 
                xceng=3001., yceng=3001., outdir='.', tmpdir='tmp'):
    # initial detection of main galaxy with SExtractor for re-centering purposes
        if outdir!='.':
            if not os.path.exists(outdir):
                os.makedirs(outdir)
    
        if not os.path.exists(tmpdir):
            os.makedirs(tmpdir)
        for c in range(np.size(inimgs)):
            print 'Creating Aperture Run:', c
            subprocess.call(['sex',inimgs[c],'-c','/home/vidur/se_files/gccg.sex',
                             '-CATALOG_NAME','/home/vidur/se_files/_tmp_seobj'+str(c)+'.cat',
                             '-PARAMETERS_NAME','/home/vidur/se_files/gccg_ell.param',
                             '-FILTER_NAME','/home/vidur/se_files/gccg.conv',
                             '-STARNNW_NAME','/home/vidur/se_files/gccg.nnw',
                             '-CHECKIMAGE_TYPE','APERTURES',
                             '-VERBOSE_TYPE','QUIET',
                             '-DETECT_THRESH',thresh1,
                             '-ANALYSIS_THRESH',thresh2,
                             '-WEIGHT_IMAGE',inwhts[c]],shell=True
                             )
    

Error:

    
    
    Creating Aperture Run: 0
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "fetch_swarp2.py", line 110, in objmask
        '-WEIGHT_IMAGE',inwhts[c]],
      File "/usr/lib/python2.7/subprocess.py", line 493, in call
        return Popen(*popenargs, **kwargs).wait()
      File "/usr/lib/python2.7/subprocess.py", line 679, in __init__
        errread, errwrite)
      File "/usr/lib/python2.7/subprocess.py", line 1249, in _execute_child
        raise child_exception
    OSError: [Errno 2] No such file or directory
    

I have a folder named se_files in my home directory. Its path is
/home/username/se_files. This is on Ubuntu, 12.04 32-bit.


Answer: The problem is that `subprocess` can't `sex`, presumably because it's not on
your PATH.

The [`call`](http://docs.python.org/2/library/subprocess.html#subprocess.call)
function won't ever raise an Exception because the program it ran returned
failure; it just returns that failure to you as a number. It only raises it it
can't run the program in the first place.

You can see the difference pretty easily:

    
    
    >>> import subprocess
    >>> subprocess.call(['nosuchprogram'])
    [long traceback skipped]
    FileNotFoundError: [Errno 2] No such file or directory: 'nosuchprogram'
    >>> subprocess.call(['ls', 'nosuchfile'])
    ls: nosuchfile: No such file or directory
    1
    

The first one raises; the second returns `1`.

So, put the absolute path to `sex` in the call, or make sure it's installed
properly, or make sure your script is running with the right environment
(e.g., maybe you added `/opt/sex/bin` to your `PATH` only in interactive
scripts, or you only added it for your own user but you're trying to run the
script as `nobody`, or…).

