简体   繁体   English

没有sudo,python脚本无法运行-为什么?

[英]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.pysudo 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

Check this out 看一下这个

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 installpython 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.

相关问题 为什么在有和没有sudo的情况下运行Python时PYTHONPATH有何不同? - Why is the PYTHONPATH different when running Python with and without sudo? Python 脚本在运行 SUDO 命令时抛出 TTY Sudo 错误 - Python Script throwing TTY Sudo error when running SUDO Command 在具有 sudo 权限的远程服务器上运行 python 脚本 - Running a python script on a remote server with sudo privileges 在 lighttpd 上使用 sudo 运行 python cgi 脚本 - Running python cgi script with sudo on lighttpd 使用 sudo 运行 python 脚本时出现无效语法错误 - Invalid Syntax error for running python script with sudo 使用/不使用 sudo 运行 python 程序的一部分 - Running part of python program with/without sudo 没有 sudo 无法运行 python gstreamer 脚本 - Unable to run python gstreamer script without sudo 以root身份运行Python脚本(使用sudo)-有效用户的用户名是什么? - Running Python script as root (with sudo) - what is the username of the effective user? 在Apache服务器上运行具有Sudo权限的Python CGI脚本 - Running a Python CGI Script with Sudo Privileges on the Apache Server 在KDE中使用sudo运行时,python脚本中的pynotify出错 - pynotify in python script go wrong when running with sudo in KDE
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM