[英]python script is not running without sudo - why?
This is my python script which downloads the most recent image from my S3 bucket. 这是我的python脚本,可从S3存储桶中下载最新图像。 When I run this script using
sudo python script.py
it does as expected, but not when I run it as python script.py
. 当我使用
sudo python script.py
运行此脚本时,它按预期运行,但是当我以python script.py
运行时却没有。 In this case, the script finishes cleanly without exceptions or process lockup, but there is no image file. 在这种情况下,脚本可以干净地完成,没有异常或进程锁定,但是没有映像文件。
Why does this happen? 为什么会这样? Is there something I could do at boto's library end or any other thing?
我可以在boto的库端执行其他操作吗?
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()
Presumably the problem is that you try to store things somewhere your user doesn't have permission to. 大概的问题是您试图将内容存储在用户没有权限的地方。 The second issue is that your script is hiding the error.
第二个问题是您的脚本隐藏了错误。 The
except
block completely ignores what sort of exceptions occur (and of course consumes them so you never see them), and uses logging.info
which by default doesn't show; except
块完全忽略了发生哪种异常(当然会消耗掉它们,因此您永远不会看到它们),并使用logging.info
,默认情况下它不会显示; that should probably be at least a warning and would be better if it showed something about what went wrong. 这可能至少应该是一个警告,并且如果它显示出什么地方出了问题,将会更好。 By the way, odds are you shouldn't be posting S3 authentication keys here.
顺便说一句,您不应该在这里发布S3身份验证密钥。
As @Nearoo in comments had suggested to use except Exception as inst
to catch exceptions. 正如@Nearoo在评论中建议使用
except Exception as inst
来捕获异常。
catching the exception like this 捕获这样的异常
except Exception as inst:
print(type(inst))
print(inst.args)
print(inst)
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. 如果脚本是使用python 3x编译的,则应该获取此错误。如果您使用python 2.7编译脚本,则不会出现此错误。
Probably you might be having multiple versions of python on your system, which should be the reason for your difference in behavior of python script.py
and sudo python script.py
and for the same @mootmoot answer suggests to use virtualenv
可能您的系统上可能有多个版本的python,这应该是造成
python script.py
和sudo python script.py
行为不同的原因,并且对于同一@mootmoot答案,建议使用virtualenv
'cmp' is an invalid keyword argument for this function
. 'cmp' is an invalid keyword argument for this function
。
Now if you would have googled this error, you should find that cmp
has been deprecated in python 3x and suggestions would be to use key
instead. 现在,如果您已经用谷歌搜索了此错误,则应该发现
cmp
在python 3x中已被弃用,建议使用key
代替。
add these imports 添加这些进口
import functools
from functools import cmp_to_key
and replace with these 并替换为这些
key2 = sorted(l, key = cmp_to_key(lambda x,y: (x[0] > y[0]) - (x[0] < y[0])))[-1][1]
res = key2.get_contents_to_filename(key2.name)
(x[0] > y[0]) - (x[0] < y[0])
is an alternate to cmp(x[0], y[0])
(x[0] > y[0]) - (x[0] < y[0])
是cmp(x[0], y[0])
的替代
cmp
is replaced by key
and cmp_to_key
is used from functools lib
使用
key
替换cmp
,并使用functools lib
cmp_to_key
Always install virtual environment before you start development in python. 在开始使用python开发之前,请务必先安装虚拟环境 。
The issue you face is typical python newbie problem : use sudo
to install all the pypi package. 您遇到的问题是典型的python新手问题:使用
sudo
安装所有pypi软件包。
When you do sudo pip install boto3
, it will install pypi package to system workspace and only accessible by sudo
. 当您执行
sudo pip install boto3
,它将pypi软件包安装到系统工作区,并且只能由sudo
访问。 In such case, sudo python script.py
will works, because it has the rights to access the package. 在这种情况下,
sudo python script.py
将可以工作,因为它有权访问该软件包。
To resolve this issues and isolation of development environment (so you don't pollute different project with differnt pypi package), python developer will install python virtual environment (from above link), then use mkvirtualenv
to create your project workspace, run pip install
and python setup.py install
to install required package to the environment, then you can run python without the sudo python
. 为了解决此问题并隔离开发环境(因此,您不会使用不同的pypi包污染其他项目),python开发人员将安装python虚拟环境(通过上面的链接),然后使用
mkvirtualenv
创建项目工作区,运行pip install
和python setup.py install
可以将所需的软件包python setup.py install
到环境中,然后无需sudo python
即可运行sudo python
。
Python virtualenv is also deployed inside production environment for the same reason. 出于同样的原因,Python virtualenv也部署在生产环境中。
Important Note : avoid boto and boto2. 重要说明 :避免使用boto和boto2。 AWS no longer support them nor with bug fixing(AWS is not officially support boto2, use it at your own risk).
AWS不再支持它们,也不再提供错误修复(AWS尚未正式支持boto2,使用后果自负)。 Switch to boto3.
切换到boto3。
For the exceptional handling issues, @Yann Vernier has mentioned it. 对于特殊的处理问题,@ Yann Vernier提到了它。 And exception error will not logged by
logging.info
. 而且
logging.info
不会记录异常错误。 You may try with logging.debug
or simple use raise
to raise the actual exception error. 您可以尝试使用
logging.debug
或简单使用raise
来引发实际的异常错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.