How to cx Freeze Boto3 - python

I created this simple python program that sends a message to SQS and then retrieves it. It works using 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
I then cxFreeze it with this script:
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)
I then get this error when I try to run the frozen code
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
In addition to this problem, the library seem to dynamically load services that are not s3, dynamo, or one other service.
Is there a recipe to freezing boto3 ?

The error shows that a hidden (dynamic) import is taking place. If you include the module it is looking for (ConfigParser) in the list of modules you tell cx_Freeze to include, it should work. You may have to do this multiple times.
executables = [cx_Freeze.Executable("MyScript.py")]
includes = ["ConfigParser"]
buildOptions = dict(includes = includes)
cx_Freeze.setup(name, description, options = dict(build_exe = buildOptions),
executables = executables)
Once you have a working program, you can also do this instead of manipulating your specific setup.py. You can add an entry into the cx_Freeze.hooks module that looks something like this:
def load_boto3(finder, module):
finder.IncludeModule("ConfigParser")
Include any others that you discover along the way. Then create a pull request or issue over here:
https://bitbucket.org/anthony_tuininga/cx_freeze

Thanks #Anthony Tuininga
There were a few additional steps that I am posting here:
Added "ConfigParser" and "HTMLParser" as noted by 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)
export AWS_DATA_PATH=/usr/local/lib/python2.7/dist-packages/botocore/data
from:
requests.exceptions.SSLError: [Errno 2] No such file or directory
export REQUESTS_CA_BUNDLE=/usr/local/lib/python2.7/dist-packages/botocore/vendored/requests/cacert.pem
Used lower level code to use 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']
After this sqs works for the 'hello world' script posted on the OP

Related

Cannot run FMU in pyFMI?

I'm trying to simulate a FMU exported from OpenModelica using pyFMI:
from pyfmi import load_fmu
import os
baseDir = os.path.dirname(os.getcwd())
fmu = baseDir + '\\Woody\\woody.fmu'
model = load_fmu(fmu)
# Get the options
opts = model.simulate_options()
# Specify tolerances
opts["CVode_options"]["atol"] = 1e-6
opts["CVode_options"]["rtol"] = 1e-6
# Simulate
res = model.simulate(final_time=1, options=opts)
However I keep getting the following error. I use Anaconda 2 and as far as I know I have all of the required packages (as required by the project page: https://pypi.org/project/PyFMI/)
Could not find No module named sundials
Could not find No module named kinsol
Could not find cannot import name dopri5
Could not find cannot import name rodas
Could not find cannot import name odassl
Could not find ODEPACK functions.
Could not find RADAR5
Could not find GLIMDA.
Traceback (most recent call last):
File "C:\Projects\08 PIN3S 2019066\FMU\python\woody_pyfmi.py", line 17, in <module>
res = model.simulate(final_time=1, options=opts)
File "src\pyfmi\fmi.pyx", line 7654, in pyfmi.fmi.FMUModelME2.simulate
File "src\pyfmi\fmi.pyx", line 301, in pyfmi.fmi.ModelBase._exec_simulate_algorithm
File "src\pyfmi\fmi.pyx", line 295, in pyfmi.fmi.ModelBase._exec_simulate_algorithm
File "C:\ProgramData\Anaconda2\lib\site-packages\pyfmi\fmi_algorithm_drivers.py", line 265, in __init__
self._set_options()
File "C:\ProgramData\Anaconda2\lib\site-packages\pyfmi\fmi_algorithm_drivers.py", line 434, in _set_options
"The solver: "+solver+ " is unknown.")
pyfmi.common.algorithm_drivers.InvalidAlgorithmOptionException: 'Invalid algorithm options object: The solver: CVode is unknown.'
Whilst trying to figure out what could be wrong I came across the FMI Library, but I have no clue what I should do with that or how to compile it, and I have not found a comprehensive guide to do so.

python ctypes.CDLL does not load library although location is know

To run one of my script I have to use the shapely python package, but it crashes because it cannot load the libgeos library. It gives me the following error message:
Traceback (most recent call last):
File "download_nl.py", line 2, in <module>
from download_sentinel import sentinel_available, sentinel_download, download_orbits
File "/nfs/home2/gmulder/radar_database/download_sentinel.py", line 14, in <module>
from fastkml import kml
File "/home/gmulder/python_packages/fastkml/__init__.py", line 30, in <module>
from .kml import KML, Document, Folder, Placemark
File "/home/gmulder/python_packages/fastkml/kml.py", line 37, in <module>
from .geometry import Geometry
File "/home/gmulder/python_packages/fastkml/geometry.py", line 23, in <module>
from shapely.geometry import Point, LineString, Polygon
File "/home/gmulder/python_packages/shapely/geometry/__init__.py", line 4, in <module>
from .base import CAP_STYLE, JOIN_STYLE
File "/home/gmulder/python_packages/shapely/geometry/base.py", line 9, in <module>
from shapely.coords import CoordinateSequence
File "/home/gmulder/python_packages/shapely/coords.py", line 8, in <module>
from shapely.geos import lgeos
File "/home/gmulder/python_packages/shapely/geos.py", line 73, in <module>
_lgeos = load_dll('geos_c', fallbacks=['libgeos_c.so.1', 'libgeos_c.so'])
File "/home/gmulder/python_packages/shapely/geos.py", line 68, in load_dll
libname, fallbacks or []))
OSError: Could not find lib geos_c or load any of its variants ['libgeos_c.so.1', 'libgeos_c.so'].
However, the strange thing is that, when I run python interactively, the import goes fine and I do not get any errors. To check whether it is a problem with setting the right paths, I also checked the source. The original code is the following, where the libname gives the library name.
def load_dll(libname, fallbacks=None, mode=DEFAULT_MODE):
lib = find_library(libname)
dll = None
print(sys.version)
dirname = '/hpc/sw/geos-3.5.0/lib'
os.environ['PATH'] = dirname + os.pathsep + os.environ['PATH']
if os.path.exists(os.path.join(dirname, 'libgeos_c.so.1')):
print('library does exist!')
os.chdir(dirname)
if lib is not None:
try:
LOG.debug("Trying `CDLL(%s)`", lib)
dll = CDLL(lib)
except OSError:
LOG.warn("Failed `CDLL(%s)`", lib)
pass
if not dll and fallbacks is not None:
for name in fallbacks:
try:
LOG.debug("Trying `CDLL(%s)`", name)
dll = CDLL(name)
except OSError:
# move on to the next fallback
LOG.warn("Failed `CDLL(%s)`", name)
pass
print(dll)
if dll:
LOG.debug("Library path: %r", lib or name)
LOG.debug("DLL: %r", dll)
return dll
else:
# No shared library was loaded. Raise OSError.
raise OSError(
"Could not find lib {0} or load any of its variants {1}.".format(
libname, fallbacks or []))
To check whether the needed lib file location can be found by the code I added an extra check at line 7, which confirms the library is there. But then, when I try to load it, nothing is loaded, even if I feed the full path to the CDLL function directly.
I also tried to load the library by running python and opening the library interactively (not from a python script), which worked fine. Therefore, I checked the python versions, because I thought that could maybe cause this problem. But the python versions for my script and the interactive session are the same. Can someone help me with this? Likely I missed something, but I have no idea what is causing this problem.

cx_freeze and docx - problems when freezing

I have a simple program that takes input from the user and then does scraping with selenium. Since the user doesn't have Python environment installed I would like to convert it to *.exe. I usually use cx_freeze for that and I have successfully converted .py programs to .exe. At first it was missing some modules (like lxml) but I was able to solve it. Now I think I only have problem with docx package.
This is how I initiate the new document in my program (I guess this is what causes me problems):
doc = Document()
#then I do some stuff to it and add paragraph and in the end...
doc.save('results.docx')
When I run it from python everything works fine but when I convert to exe I get this error:
Traceback (most recent call last):
File "C:\Users\tyszkap\AppData\Local\Continuum\Anaconda3\lib\site-packages\cx_Freeze\initscripts\Console.py", line 27, in <module>
exec(code, m.__dict__)
File "tribunalRio.py", line 30, in <module>
File "C:\Users\tyszkap\AppData\Local\Continuum\Anaconda3\lib\site-packages\docx\api.py", line 25, in Document
document_part = Package.open(docx).main_document_part
File "C:\Users\tyszkap\AppData\Local\Continuum\Anaconda3\lib\site-packages\docx\opc\package.py", line 116, in open
pkg_reader = PackageReader.from_file(pkg_file)
File "C:\Users\tyszkap\AppData\Local\Continuum\Anaconda3\lib\site-packages\docx\opc\pkgreader.py", line 32, in from_file
phys_reader = PhysPkgReader(pkg_file)
File "C:\Users\tyszkap\AppData\Local\Continuum\Anaconda3\lib\site-packages\docx\opc\phys_pkg.py", line 31, in __new__
"Package not found at '%s'" % pkg_file
docx.opc.exceptions.PackageNotFoundError: Package not found at 'C:\Users\tyszkap\Dropbox (Dow Jones)\Python Projects\build\exe.win-a
md64-3.4\library.zip\docx\templates\default.docx'
This is my setup.py program:
from cx_Freeze import setup, Executable
executable = Executable( script = "tribunalRio.py" )
# Add certificate to the build
options = {
"build_exe": {'include_files' : ['default.docx'],
'packages' : ["lxml._elementpath", "inspect", "docx", "selenium"]
}
}
setup(
version = "0",
requires = [],
options = options,
executables = [executable])
I thought that explicitly adding default.docx to the package would solve the problem (I have even tried adding it to the library.zip but it gives me even more errors) but it didn't. I have seen this post but I don't know what they mean by:
copying the docx document.py module inside my function (instead of
using Document()
Any ideas? I know that freezing is not the best solution but I really don't want to build a web interface for such a simple program...
EDIT:
I have just tried this solution :
def find_data_file(filename):
if getattr(sys, 'frozen', False):
# The application is frozen
datadir = os.path.dirname(sys.executable)
else:
# The application is not frozen
# Change this bit to match where you store your data files:
datadir = os.path.dirname(__file__)
return os.path.join(datadir, filename)
doc = Document(find_data_file('default.docx'))
but again receive Traceback error (but the file is in this location...):
Traceback (most recent call last):
File "C:\Users\tyszkap\AppData\Local\Continuum\Anaconda3\lib\site-packages\cx_Freeze\initscripts\Console.py", line 27, in <module>
exec(code, m.__dict__)
File "tribunalRio.py", line 43, in <module>
File "C:\Users\tyszkap\AppData\Local\Continuum\Anaconda3\lib\site-packages\docx\api.py", line 25, in Document
document_part = Package.open(docx).main_document_part
File "C:\Users\tyszkap\AppData\Local\Continuum\Anaconda3\lib\site-packages\docx\opc\package.py", line 116, in open
pkg_reader = PackageReader.from_file(pkg_file)
File "C:\Users\tyszkap\AppData\Local\Continuum\Anaconda3\lib\site-packages\docx\opc\pkgreader.py", line 32, in from_file
phys_reader = PhysPkgReader(pkg_file)
File "C:\Users\tyszkap\AppData\Local\Continuum\Anaconda3\lib\site-packages\docx\opc\phys_pkg.py", line 31, in __new__
"Package not found at '%s'" % pkg_file
docx.opc.exceptions.PackageNotFoundError: Package not found at 'C:\Users\tyszkap\Dropbox (Dow Jones)\Python Projects\build\exe.win-a
md64-3.4\default.docx'
What am I doing wrong?
I expect you'll find the problem has to do with your freezing operation not placing the default Document() template in the expected location. It's stored as package data in the python-docx package as docx/templates/default.docx (see setup.py here: https://github.com/python-openxml/python-docx/blob/master/setup.py#L37)
I don't know how to fix that in your case, but that's where the problem is it looks like.
I had the same problem and managed to get around it by doing the following. First, I located the default.docx file in the site-packages. Then, I copied it in the same directory as my .py file. I also start the .docx file with Document() which has a docx=... flag, to which I assigned the value: os.path.join(os.getcwd(), 'default.docx') and now it looks like doc = Document(docx=os.path.join(os.getcwd(), 'default.docx')). The final step was to include the file in the freezing process. Et voilĂ ! So far I have no problem.

Python program showing exception

I want to run this below program using python..
from __future__ import print_function
import sys
from OCC.STEPControl import STEPControl_Reader
from OCC.IFSelect import IFSelect_RetDone, IFSelect_ItemsByEntity
from OCC.Display.SimpleGui import init_display
from OCC.Display.WebGl import threejs_renderer
from OCC.STEPCAFControl import STEPCAFControl_Reader
step_reader = STEPControl_Reader()
status = step_reader.ReadFile('./models/part_ARROWS_ASM.stp')
if status == IFSelect_RetDone: # check status
failsonly = False
step_reader.PrintCheckLoad(failsonly, IFSelect_ItemsByEntity)
step_reader.PrintCheckTransfer(failsonly, IFSelect_ItemsByEntity)
ok = step_reader.TransferRoot(1)
_nbs = step_reader.NbShapes()
aResShape = step_reader.Shape(1)
else:
print("Error: can't read file.")
sys.exit(0)
#display, start_display, add_menu, add_function_to_menu = init_display()
#display.DisplayShape(aResShape, update=True)
#start_display()
my_renderer = threejs_renderer.ThreejsRenderer(background_color="#123345")
my_renderer.DisplayShape(aResShape)
Its running successfully without this module OCC.STEPCAFControl. After running above program found this below eeror.
Traceback (most recent call last):
File "D:\software\python\pythonocc-core-master\examples\core_load_step_ap203.py", line 26, in <module>
from OCC.STEPCAFControl import STEPCAFControl_Reader
ImportError: No module named 'OCC.STEPCAFControl'
After got this error paste OCC.STEPCAFControl this module within OCC folder and this problem is solved but its showing another exception which is mentioned below.
Traceback (most recent call last):
File "D:\software\python\pythonocc-core-master\examples\core_load_step_ap203.py", line 26, in <module>
from OCC.STEPCAFControl import STEPCAFControl_Reader
File "C:\Python34\lib\site-packages\OCC\STEPCAFControl.py", line 28, in <module>
_STEPCAFControl = swig_import_helper()
File "C:\Python34\lib\site-packages\OCC\STEPCAFControl.py", line 24, in swig_import_helper
_mod = imp.load_module('_STEPCAFControl', fp, pathname, description)
File "C:\Python34\lib\imp.py", line 243, in load_module
return load_dynamic(name, filename, file)
ImportError: DLL load failed: The specified module could not be found.
How to solve this issue? Any suggestion?
The python module is probably trying to load a DLL called _STEPCAFControl.pyd
(I'm guessing at the extension). If you have a STEPCAFControl.dll with this name, you can rename it _STEPCAFControl.pyd, and it may work. If the names is not correct, you need to look into the STEPCAFControl.py file, and find the .DLL file name.
For scientific python on windows, your best bet is to install WinPython, Python(x,y), Enthought Python or Anaconda rather than trying to install everything manually. Any of these will install most packages you are likely to need, and should configure them correctly.

cx_freeze fails to bundle django.utils.six.moves

I am trying to bundle django with cx_freeze. With my setup.py I am able to bundle it together but when I invoke the generated executable I get the below import error. I have tried various ways to fix this but unable to get a way around.
Versions used:
Django==1.6.4
cx-Freeze==4.3.3
./build/exe.linux-x86_64-2.7/script
Traceback (most recent call last):
File "lib/python2.7/site-packages/cx_Freeze/initscripts/Console.py", line 27, in <module>
exec(code, m.__dict__)
File "script.py", line 7, in <module>
from models import Project
File "/remote/vgrnd77/pritam/tryout/package/models.py", line 6, in <module>
from django.db.models import CharField, Model, \
File "lib/python2.7/site-packages/django/db/models/__init__.py", line 5, in <module>
from django.db.models.query import Q
File "lib/python2.7/site-packages/django/db/models/query.py", line 14, in <module>
from django.db.models.fields import AutoField
File "lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 15, in <module>
from django import forms
File "lib/python2.7/site-packages/django/forms/__init__.py", line 8, in <module>
from django.forms.fields import *
File "lib/python2.7/site-packages/django/forms/fields.py", line 17, in <module>
from django.forms.util import ErrorList, from_current_timezone, to_current_timezone
File "lib/python2.7/site-packages/django/forms/util.py", line 4, in <module>
from django.utils.html import format_html, format_html_join
File "lib/python2.7/site-packages/django/utils/html.py", line 12, in <module>
from django.utils.text import normalize_newlines
File "lib/python2.7/site-packages/django/utils/text.py", line 11, in <module>
from django.utils.six.moves import html_entities
ImportError: cannot import name html_entities
Here is my sample script:
#!/usr/bin/env python
import os
import django
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
from models import Project
if __name__ == '__main__':
print "My first exe"
p = Project.objects.all()
for project in p:
print project.name
Here is how the setup.py looks like:
main_python_file = "script.py"
import sys
from cx_Freeze import setup, Executable
# html_entities is missing
base = 'Console'
buildOptions = dict(
create_shared_zip = False,
append_script_to_exe = False,
packages = [],
includes = ['django'],
excludes = ['tkinter']
)
setup(
name = "my_exe",
version = "0.1",
description = "My first exe",
options = dict(build_exe = buildOptions),
executables = [Executable(main_python_file, base = base)])
Here is how the project table looks like:
class Project(Model):
"""
Project table
"""
name = CharField(max_length=255)
Has anyone faced and solved this ?
Freeze Output shows it was not able to figure out django.utils.six.moves. It shows 'six' in missing modules:
Missing modules:
? django.utils.six.moves imported from django.db.backends, django.db.models.base, django.db.models.sql.where, django.dispatch.dispatcher, django.forms.formsets, django.http.cookie, django.http.response, django.utils.crypto, django.utils.functional, django.utils.html_parser, django.utils.ipv6, django.utils.regex_helper, django.utils.text
? django.utils.six.moves.urllib.parse imported from django.core.files.storage, django.core.validators, django.forms.fields, django.forms.widgets, django.http.request, django.http.response, django.utils.encoding, django.utils.html, django.utils.http
If you look in the django.utils.six file you will find the following code:
...
MovedModule("http_cookiejar", "cookielib", "http.cookiejar"),
MovedModule("http_cookies", "Cookie", "http.cookies"),
MovedModule("html_entities", "htmlentitydefs", "html.entities"),
MovedModule("html_parser", "HTMLParser", "html.parser"),
...
What django does dynamically is build up the django.utils.six.moves module out of thin air. The parameters to MovedModule are . So the solution in python33 (the one I used) is to import html.entities in my Executable module. I tried to add it to the includes for the executable, but that didn't seem to work. I assume you could use the import htmlenditydefs for the python2 version.

Categories

Resources