Py2exe does not recognize jsonschema

I am trying to create a Windows executable with py2exe for a Python program that uses the jsonschema package, but every time I try to run the executable it fails with the following error:

File "jsonschema\__init__.pyc", line 18, in <module>
File "jsonschema\validators.pyc", line 163, in <module>
File "jsonschema\_utils.pyc", line 57, in load_schema
File "pkgutil.pyc", line 591, in get_data
IOError: [Errno 0] Error: 'jsonschema\\schemas\\draft3.json'

      

I tried adding json and jsonschema to the package options for py2exe in setup.py and I also tried to manually copy the jsonschema directory from my location in Python27 \ Libs \ site-packages to library.zip, but none of these work. I also tried using the solution I found here ( http://crazedmonkey.com/blog/python/pkg_resources-with-py2exe.html ), which assumes the py2exe extension to be able to copy files to a zip file, but that doesn't seem to work.

I'm guessing this is because py2exe only includes the Python files in library.zip, but I was wondering if there is a way for this to work without converting draft3.json and draft4.json to .py files at their original location.

Thank you in advance

+3


source to share


1 answer


Well, after a few more searches (I hate ugly), I got it without fixing the build_exe.py file. The key to this was the recipe http://crazedmonkey.com/blog/python/pkg_resources-with-py2exe.html . My collector class looks like this:

from py2exe.build_exe import py2exe as build_exe

class JsonSchemaCollector(build_exe):
   """
       This class Adds jsonschema files draft3.json and draft4.json to
       the list of compiled files so it will be included in the zipfile.
   """

    def copy_extensions(self, extensions):
        build_exe.copy_extensions(self, extensions)

        # Define the data path where the files reside.
        data_path = os.path.join(jsonschema.__path__[0], 'schemas')

        # Create the subdir where the json files are collected.
        media = os.path.join('jsonschema', 'schemas')
        full = os.path.join(self.collect_dir, media)
        self.mkpath(full)

        # Copy the json files to the collection dir. Also add the copied file
        # to the list of compiled files so it will be included in the zipfile.
        for name in os.listdir(data_path):
            file_name = os.path.join(data_path, name)
            self.copy_file(file_name, os.path.join(full, name))
            self.compiled_files.append(os.path.join(media, name))

      

It remains to add it to the base installation as follows:



options = {"bundle_files": 1,    # Bundle ALL files inside the EXE
           "compressed": 2,      # compress the library archive
           "optimize": 2,        # like python -OO
           "packages": packages, # Packages needed by lxml.
           "excludes": excludes, # COM stuff we don't want
           "dll_excludes": skip} # Exclude unused DLLs

distutils.core.setup(
    cmdclass={"py2exe": JsonSchemaCollector},
    options={"py2exe": options},
    zipfile=None,
    console=[prog])

      

Some of the code is omitted as they are not relevant in this context, but I think you got a drift.

+2


source







All Articles