python script is not running without sudo - why?
<p>This is my python script which downloads the most recent image from my S3 bucket. When I run this script using <code>sudo python script.py</code> it does as expected, 
but not when I run it as <code>python script.py</code>. In this case, the script finishes cleanly without exceptions or process lockup, but there is no image file.</p>

<p>Why does this happen? Is there something I could do at boto's library end or any other thing?</p>

<pre><code>import boto
import logging


def s3_download():
    bucket_name = 'cloudadic'
    conn = boto.connect_s3('XXXXXX', 'YYYYYYY')
    bucket = conn.get_bucket(bucket_name)

    for key in bucket.list('ocr/uploads'):
        try:
            l = [(k.last_modified, k) for k in bucket]
            key = sorted(l, cmp=lambda x, y: cmp(x[0], y[0]))[-1][1]
            res = key.get_contents_to_filename(key.name)
        except:
            logging.info(key.name + ":" + "FAILED")

if __name__ == "__main__":
     s3_download()
</code></pre>

<p>As @Nearoo in comments had suggested to use <code>except Exception as inst</code> to catch exceptions. </p>

<p>catching the exception like this </p>

<pre><code>except Exception as inst:
            print(type(inst))
            print(inst.args)
            print(inst)
</code></pre>

<p>Should fetch you this error if the script is compiled using python 3x
If you would compile your script using python 2.7, this error wouldn't come.</p>

<p>Probably you might be having multiple versions of python on your system,
which should be the reason for your difference in behavior of <code>python script.py</code> and <code>sudo python script.py</code> and for the same @mootmoot answer suggests to use <code>virtualenv</code></p>

<p><code>'cmp' is an invalid keyword argument for this function</code>. </p>

<p>Now if you would have googled this error, you should find that <code>cmp</code> has been deprecated in python 3x and suggestions would be to use <code>key</code> instead.</p>

<p>add these imports</p>

<pre><code>import functools
from functools import cmp_to_key
</code></pre>

<p>and replace 
with these</p>

<pre><code>key2 = sorted(l, key = cmp_to_key(lambda x,y: (x[0] &gt; y[0]) - (x[0] &lt; y[0])))[-1][1]
res = key2.get_contents_to_filename(key2.name)
</code></pre>

<p><code>(x[0] &gt; y[0]) - (x[0] &lt; y[0])</code> is an alternate to <code>cmp(x[0], y[0])</code></p>

<p><code>cmp</code> is replaced by <code>key</code> and <code>cmp_to_key</code> is used from functools <code>lib</code> </p>

<p><a href="http://stackoverflow.com/questions/20202418/why-is-the-cmp-parameter-removed-from-sort-sorted-in-python3-0">Check this out</a></p>

python boto sudo