py2exe无法识别jsonschem

2024-09-29 23:29:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我一直试图用py2exe为使用jsonschema包的Python程序构建一个Windows可执行文件,但每次尝试运行该可执行文件时都会失败,并出现以下错误:

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'

我尝试在py2exe的包选项中添加json和jsonschema设置.py我还尝试手动将jsonschema目录从Python27\Libs\site包中的位置复制到图书馆.zip但这两种方法都不管用。我还尝试使用这里找到的解决方案(http://crazedmonkey.com/blog/python/pkg_resources-with-py2exe.html),它建议扩展py2exe以便能够将文件复制到zip文件中,但这似乎也不起作用。在

我假设发生这种情况是因为py2exe只在图书馆.zip,但我想知道是否有任何方法可以在不需要将draft3.json和draft4.json转换为原始位置的.py文件的情况下工作。在

提前谢谢你


Tags: 文件方法inpyjson可执行文件图书馆line
1条回答
网友
1楼 · 发布于 2024-09-29 23:29:52

好吧,经过更多的谷歌搜索(我讨厌难看的),我在没有修补构建的情况下让它正常工作_exe.py文件文件。整件事的关键是http://crazedmonkey.com/blog/python/pkg_resources-with-py2exe.html的配方。我的收集器类如下所示:

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))

剩下的就是将其添加到核心设置中,如下所示:

^{pr2}$

有些代码被省略了,因为它与上下文无关,但我想你明白了。在

相关问题 更多 >

    热门问题