Python py2exe不识别jsonschema
我一直在尝试为使用jsonschema包的Python程序使用py2exe构建Windows可执行文件,但每次尝试运行该可执行文件时,都会失败,并出现以下错误:Python py2exe不识别jsonschema,python,json,py2exe,jsonschema,Python,Json,Py2exe,Jsonschema,我一直在尝试为使用jsonschema包的Python程序使用py2exe构建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_s
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'
文件“jsonschema\\uuuu init\uuuuu.pyc”,第18行,在
文件“jsonschema\validators.pyc”,第163行,在
加载模式中的第57行文件“jsonschema\\ u utils.pyc”
get_数据中第591行的文件“pkgutil.pyc”
IOError:[Errno 0]错误:“jsonschema\\schemas\\draft3.json”
我尝试将json和jsonschema添加到setup.py中py2exe的包选项中,还尝试将jsonschema目录从Python27\Libs\site包中的位置手动复制到library.zip中,但这两种方法都不起作用。我还尝试使用此处找到的解决方案(),该解决方案建议扩展py2exe,以便能够将文件复制到zip文件中,但这似乎也不起作用
我假设这是因为py2exe在library.zip中只包含Python文件,但我想知道是否有任何方法可以让它工作,而不必在原始位置将draft3.json和draft4.json转换为.py文件
提前谢谢你在谷歌搜索了一段时间后(我讨厌丑陋),我在没有修补build_exe.py文件的情况下就成功了。整个事情的关键是当时的配方。我的收集器类如下所示:
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))
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])
剩下的就是将其添加到核心设置中,如下所示:
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))
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])
有些代码被省略了,因为它在这个上下文中不相关,但我想你明白了