如何 cx 冻结 Boto3

How to cx Freeze Boto3

我创建了这个简单的 python 程序,它向 SQS 发送消息然后检索它。它使用 python 2.7.11 工作。

import boto3 
sqs = boto3.client('sqs')
queue = sqs.get_queue_by_name(QueueName='some-que-name')
queue.send_message(MessageBody='{"phrase": "It\'s the end of the world    as we know it" }' )
for message in queue.receive_messages():
    print message.body

然后我用这个脚本 cxFreeze 它:

from cx_Freeze import setup, Executable

include_mods = []

excludes = ['tkinter', 'cltk']

buildOptions = dict(packages=[], excludes=excludes, includes=include_mods)


executables = [
     Executable('./frozen_boto_3_test.py', 'Console')
]

setup(name='Boto3FrozenTest',
  version='1',
  description='A test to make sure boto3 is working well when frozen',
  options=dict(build_exe=buildOptions),
  executables=executables)

然后当我尝试 运行 冻结代码

时出现此错误
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/site-packages/cx_Freeze/initscripts/Console.py", line 27, in <module>
    exec(code, m.__dict__)
  File "./frozen_boto_3_test.py", line 1, in <module>
    import boto3 
  File "/usr/local/lib/python2.7/site-packages/boto3/__init__.py", line 16, in <module>
    from boto3.session import Session
  File "/usr/local/lib/python2.7/site-packages/boto3/session.py", line 17, in <module>
    import botocore.session
  File "/usr/local/lib/python2.7/site-packages/botocore/session.py", line 25, in <module>
    import botocore.configloader
  File "/usr/local/lib/python2.7/site-packages/botocore/configloader.py", line 18, in <module>
    from six.moves import configparser
  File "/usr/local/lib/python2.7/site-packages/six.py", line 203, in load_module
    mod = mod._resolve()
  File "/usr/local/lib/python2.7/site-packages/six.py", line 115, in _resolve
    return _import_module(self.mod)
  File "/usr/local/lib/python2.7/site-packages/six.py", line 82, in _import_module
    __import__(name)
ImportError: No module named ConfigParser

除了这个问题,库似乎动态加载不是 s3、dynamo 或其他服务的服务。

有冻结boto3的方法吗?

错误表明正在进行隐藏(动态)导入。如果您将它正在寻找的模块 (ConfigParser) 包含在您告诉 cx_Freeze 要包含的模块列表中,它应该可以工作。您可能需要多次执行此操作。

executables = [cx_Freeze.Executable("MyScript.py")]
includes = ["ConfigParser"]
buildOptions = dict(includes = includes)
cx_Freeze.setup(name, description, options = dict(build_exe = buildOptions),
        executables = executables)

一旦你有一个工作程序,你也可以这样做而不是操纵你的特定 setup.py。您可以在 cx_Freeze.hooks 模块中添加一个条目,如下所示:

def load_boto3(finder, module):
    finder.IncludeModule("ConfigParser")

包括您在此过程中发现的任何其他内容。然后在此处创建拉取请求或问题:

https://bitbucket.org/anthony_tuininga/cx_freeze

感谢@Anthony Tuininga

我在这里发布了一些额外的步骤:

已添加 "ConfigParser" 和 "HTMLParser",如 Anthony

所述
from cx_Freeze import setup, Executable

include_mods = ["ConfigParser", "HTMLParser"]

excludes = ['tkinter', 'cltk']

buildOptions = dict(packages=[], excludes=excludes, includes=include_mods)


executables = [
     Executable('./frozen_boto_3_test.py', 'Console')
]

setup(name='Boto3FrozenTest',
  version='1',
  description='A test to make sure boto3 is working well when frozen',
  options=dict(build_exe=buildOptions),
  executables=executables)

导出 AWS_DATA_PATH=/usr/local/lib/python2.7/dist-packages/botocore/data 从: requests.exceptions.SSLError: [Errno 2] No such file or directory

导出REQUESTS_CA_BUNDLE=/usr/local/lib/python2.7/dist-packages/botocore/vendored/requests/cacert.pem

使用较低级别的代码来使用 sqs:

import boto3 

sqs = boto3.client('sqs')

queue = sqs.create_queue( QueueName="my-queue" )
sqs.send_message(QueueUrl=queue["QueueUrl"] , MessageBody='{"phrase": "It\'s the end of the world as we know it" }' )
message = sqs.receive_message(QueueUrl=queue["QueueUrl"])
for msg in message['Messages']: 
    print m

sg['Body']

此 sqs 对 OP 上发布的 'hello world' 脚本起作用后