如何编译使用boto访问S3的python代码?

发布于 2024-09-25 07:03:30 字数 2111 浏览 6 评论 0原文

我正在尝试编译一个简单的 Python 程序,该程序使用 boto 包将文件上传到 S3 存储桶,并生成单个可再发行的 .exe 文件。我对任何编译方法都持开放态度。到目前为止,我已经尝试了 bbfreeze 和 py2exe ,并且都产生了相同的结果。导致问题的相关代码如下所示:

import boto
#...snip...
fname_base = os.path.basename(fname)
s3 = boto.connect_s3(aws_access_key_id=_aws_key, aws_secret_access_key=_aws_secret_key, is_secure=False);
bucket = s3.get_bucket(_bucket)
key = bucket.new_key(fname_base)
key.set_contents_from_filename(fname)

使用任一可执行捆绑实用程序编译并运行我得到:

Traceback (most recent call last):
  File "s3stash.py", line 238, in <module>
    sys.exit(main())
  File "s3stash.py", line 225, in main
    push_file_to_s3(f, options)
  File "s3stash.py", line 160, in push_file_to_s3
    _push_with_boto(fname)
  File "s3stash.py", line 148, in _push_with_boto
    s3 = boto.connect_s3(aws_access_key_id=_aws_key, aws_secret_access_key=_aws_secret_key, is_secure=False);
  File "boto\__init__.pyo", line 104, in connect_s3
  File "zipextimporter.pyo", line 82, in load_module
  File "boto\s3\connection.pyo", line 27, in <module>
  File "zipextimporter.pyo", line 82, in load_module
  File "boto\utils.pyo", line 55, in <module>
  File "email\__init__.pyo", line 79, in __getattr__
ImportError: No module named multipart

我在 Windows XP SP3 上使用 ActiveState Python 2.6。 boto 包的安装方式是:

easy_installer --always-unzip boto

我根据信息使用了 --always-unzip 选项 在这里找到关于 py2exe 解压的 Egg 文件存在问题的信息。不幸的是,当我使用 bb-freeze 构建可执行文件时,我得到的错误是相同的。

py2exe 的输出在接近结尾处包括以下信息:

The following modules appear to be missing
['_scproxy', 'email.Encoders', 'email.MIMEBase', 'email.MIMEMultipart', 'email.MIMEText', 'email.Utils', 'simplejson']

这提供了一些提示。我尝试了其他帖子中建议的方法,其中在使用 py2exe 编译时建议使用 -i 选项,但不幸的是没有任何帮助。在其他问题中,用户自己明确包含了电子邮件子模块。不幸的是,我无法弄清楚如何使这些解决方案适应我的情况,只是用 -i 添加它们并没有阻止 py2exe 警告我缺少模块,或者生成的结果捆绑的 exe 因缺少模块错误而失败。

有人可以帮我捆绑此代码以进行重新分发吗?

I'm trying to compile a simple Python program, that uploads files to an S3 bucket using the boto package, in to a single, redistributable .exe file. I'm open to any compilation method. So far I've tried both bbfreeze and py2exe and both yield the same results. The code in question that causes trouble looks like this:

import boto
#...snip...
fname_base = os.path.basename(fname)
s3 = boto.connect_s3(aws_access_key_id=_aws_key, aws_secret_access_key=_aws_secret_key, is_secure=False);
bucket = s3.get_bucket(_bucket)
key = bucket.new_key(fname_base)
key.set_contents_from_filename(fname)

Compiled with either executable bundling utility and run I get:

Traceback (most recent call last):
  File "s3stash.py", line 238, in <module>
    sys.exit(main())
  File "s3stash.py", line 225, in main
    push_file_to_s3(f, options)
  File "s3stash.py", line 160, in push_file_to_s3
    _push_with_boto(fname)
  File "s3stash.py", line 148, in _push_with_boto
    s3 = boto.connect_s3(aws_access_key_id=_aws_key, aws_secret_access_key=_aws_secret_key, is_secure=False);
  File "boto\__init__.pyo", line 104, in connect_s3
  File "zipextimporter.pyo", line 82, in load_module
  File "boto\s3\connection.pyo", line 27, in <module>
  File "zipextimporter.pyo", line 82, in load_module
  File "boto\utils.pyo", line 55, in <module>
  File "email\__init__.pyo", line 79, in __getattr__
ImportError: No module named multipart

I'm using ActiveState Python 2.6 on Windows XP SP3. The boto package was installed with:

easy_installer --always-unzip boto

I used the --always-unzip option based on the information found here about py2exe having issues with the egg files that were unpacked. Unfortunately the error I get is the same when I use bb-freeze to build the executable.

The output from py2exe includes, near the end, the following bit of information:

The following modules appear to be missing
['_scproxy', 'email.Encoders', 'email.MIMEBase', 'email.MIMEMultipart', 'email.MIMEText', 'email.Utils', 'simplejson']

Which lends some hints. I tried methods suggested in other posts to SO where the -i option was recommended when compiling with py2exe and unfortunately nothing helped. In those other questions the users were doing their own explicit inclusion of of the email sub-modules. I could not figure how to adapt those solutions to my case unfortunately and just adding them with -i didn't stop py2exe from warning me of the missing modules, or the resulting bundled exe from failing with the missing module error.

Can someone help me get this code bundled for redistribution?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

要走干脆点 2024-10-02 07:03:30

我实际上做到了这一点。答案是放弃 boto 并使用海报库。我仍然使用 boto 生成签名策略以及通过海报执行的 POST 所需的表单字段,但执行 POST 的实际可执行文件现在仅包括海报。仅将海报混合在一起,py2exe 就可以为我创建独立的可执行文件以进行重新分发。

I actually got this to work. The answer was to ditch boto and use the poster library instead. I still use boto to generate a signed policy and the necessary form fields for POST I do via poster, but the actual executable that does the POST only includes poster now. With just poster in the mix, py2exe doesn't have any issues creating a standalone executable for me for redistribution.

离线来电— 2024-10-02 07:03:30

我知道这是一个老问题,但我遇到了同样的问题并在仍然使用 py2exe 和 py2app 的同时修复了它。只需在 setup.py 中使用“packages”选项而不是“includes”选项:

extra = {}
mainscript = "foo/__main__.py"

if is_os_x():
    extra["setup_requires"] =['py2app']
    extra["app"] = [mainscript]
    extra["options"] = { "py2app": {
        "packages": ['email'],
        }                                       
    }

elif is_windows():
    extra["setup_requires"] =['py2exe']
    extra['console'] = [mainscript]
    extra['options'] = {'py2exe': {
        # Includes that py2exe doesn't include automatically
       'packages': ['email'],
       }
    }

setup(
    # snip
    ** extra
)   

希望有帮助。

I know this is an old question, but I had the same problem and fixed it while still using py2exe and py2app. Just use the 'packages' option instead of the 'includes' option in your setup.py:

extra = {}
mainscript = "foo/__main__.py"

if is_os_x():
    extra["setup_requires"] =['py2app']
    extra["app"] = [mainscript]
    extra["options"] = { "py2app": {
        "packages": ['email'],
        }                                       
    }

elif is_windows():
    extra["setup_requires"] =['py2exe']
    extra['console'] = [mainscript]
    extra['options'] = {'py2exe': {
        # Includes that py2exe doesn't include automatically
       'packages': ['email'],
       }
    }

setup(
    # snip
    ** extra
)   

Hope that helps.

用心笑 2024-10-02 07:03:30

我已经成功地用 boto & 创建了工作 exe py2exe

添加到您的 script.py

from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

下一个错误与 HTTPS 连接有关,似乎 py2exe 以某种方式“隐藏”证书文件。
解决这个问题的方法是
1)使用HTTP
或者
2) 不要检查证书

1) HTTP

conn = boto.connect_dynamodb(
    aws_access_key_id = ' ',
    aws_secret_access_key = ' ', 
    is_secure = False)

“is_secure = False”很重要

2) 不要检查证书

conn = boto.connect_dynamodb(
    aws_access_key_id = ' ',
    aws_secret_access_key = ' ', 
    is_secure = True, 
    validate_certs = False)

“validate_certs = False”很重要

如果有人弄清楚如何修复证书验证中的错误,请回复!

I've managed to create working exe with boto & py2exe

Add to your script.py

from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

Next error is with HTTPS connection, it seems that py2exe is "hiding" cert file somehow..
The way to fix this is
1) use HTTP
OR
2) dont check certs

1) HTTP

conn = boto.connect_dynamodb(
    aws_access_key_id = ' ',
    aws_secret_access_key = ' ', 
    is_secure = False)

"is_secure = False" is crutial

2) DONT CHECK CERTS

conn = boto.connect_dynamodb(
    aws_access_key_id = ' ',
    aws_secret_access_key = ' ', 
    is_secure = True, 
    validate_certs = False)

"validate_certs = False" is crutial

If someone figures out how to fix error in cert validation, please reply !

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文