Error: Unable to run code with xmlschema after build with pyinstaller

Error: Unable to run code with xmlschema after build with pyinstaller

我是 xmlschema 和 pyinstaller 的新手,需要一些帮助来解决我遇到的错误。

我已经在 Python 3.6.8 中下载了 xmlschema (1.9) 和 pyinstaller (4.7)。 在我的 python 脚本中,我添加了 import xmlschema 以便使用它。 之后,我使用 `pyinstaller --onefile --windowed schema_check_xsd_11.py 编译了我的代码以创建一个 windows exe 应用程序。

编译成功:

D:\Project\WA_Schema\Schema_Check>pyinstaller --onefile --windowed schema_check_xsd_11.py
82 INFO: PyInstaller: 4.7
82 INFO: Python: 3.6.8
84 INFO: Platform: Windows-10-10.0.17763-SP0
90 INFO: wrote D:\Project\WA_Schema\Schema_Check\schema_check_xsd_11.spec
92 INFO: UPX is not available.
93 INFO: Extending PYTHONPATH with paths
['D:\Project\WA_Schema\Schema_Check']
346 INFO: checking Analysis
361 INFO: Building because D:\Project\WA_Schema\Schema_Check\schema_check_xsd_11.py changed
362 INFO: Initializing module dependency graph...
364 INFO: Caching module graph hooks...
380 INFO: Analyzing base_library.zip ...
2806 INFO: Caching module dependency graph...
2939 INFO: running Analysis Analysis-00.toc
2941 INFO: Adding Microsoft.Windows.Common-Controls to dependent assemblies of final executable
  required by c:\python36\python.exe
3006 INFO: Analyzing D:\Project\WA_Schema\Schema_Check\schema_check_xsd_11.py
4478 INFO: Processing module hooks...
4479 INFO: Loading module hook 'hook-difflib.py' from 'c:\python36\lib\site-packages\PyInstaller\hooks'...
4481 INFO: Loading module hook 'hook-encodings.py' from 'c:\python36\lib\site-packages\PyInstaller\hooks'...
4621 INFO: Loading module hook 'hook-heapq.py' from 'c:\python36\lib\site-packages\PyInstaller\hooks'...
4622 INFO: Loading module hook 'hook-pickle.py' from 'c:\python36\lib\site-packages\PyInstaller\hooks'...
4624 INFO: Loading module hook 'hook-xml.etree.cElementTree.py' from 'c:\python36\lib\site-packages\PyInstaller\hooks'...
4628 INFO: Loading module hook 'hook-xml.py' from 'c:\python36\lib\site-packages\PyInstaller\hooks'...
4675 INFO: Looking for ctypes DLLs
4676 INFO: Analyzing run-time hooks ...
4678 INFO: Including run-time hook 'c:\python36\lib\site-packages\PyInstaller\hooks\rthooks\pyi_rth_inspect.py'
4683 INFO: Including run-time hook 'c:\python36\lib\site-packages\PyInstaller\hooks\rthooks\pyi_rth_pkgutil.py'
4689 INFO: Looking for dynamic libraries
4806 INFO: Looking for eggs
4806 INFO: Using Python library c:\python36\python36.dll
4807 INFO: Found binding redirects:
[]
4815 INFO: Warnings written to D:\Project\WA_Schema\Schema_Check\build\schema_check_xsd_11\warn-schema_check_xsd_11.txt
4855 INFO: Graph cross-reference written to D:\Project\WA_Schema\Schema_Check\build\schema_check_xsd_11\xref-schema_check_xsd_11.html
4862 INFO: checking PYZ
4878 INFO: Building because toc changed
4878 INFO: Building PYZ (ZlibArchive) D:\Project\WA_Schema\Schema_Check\build\schema_check_xsd_11\PYZ-00.pyz
5355 INFO: Building PYZ (ZlibArchive) D:\Project\WA_Schema\Schema_Check\build\schema_check_xsd_11\PYZ-00.pyz completed successfully.
5366 INFO: checking PKG
5379 INFO: Building because toc changed
5380 INFO: Building PKG (CArchive) schema_check_xsd_11.pkg
6725 INFO: Building PKG (CArchive) schema_check_xsd_11.pkg completed successfully.
6727 INFO: Bootloader c:\python36\lib\site-packages\PyInstaller\bootloader\Windows-64bit\runw.exe
6727 INFO: checking EXE
6746 INFO: Building because toc changed
6747 INFO: Building EXE from EXE-00.toc
6758 INFO: Copying bootloader EXE to D:\Project\WA_Schema\Schema_Check\dist\schema_check_xsd_11.exe
6763 INFO: Copying icon to EXE
6764 INFO: Copying icons from ['c:\python36\lib\site-packages\PyInstaller\bootloader\images\icon-windowed.ico']
6769 INFO: Writing RT_GROUP_ICON 0 resource with 104 bytes
6769 INFO: Writing RT_ICON 1 resource with 3752 bytes
6773 INFO: Writing RT_ICON 2 resource with 2216 bytes
6774 INFO: Writing RT_ICON 3 resource with 1384 bytes
6774 INFO: Writing RT_ICON 4 resource with 38188 bytes
6775 INFO: Writing RT_ICON 5 resource with 9640 bytes
6775 INFO: Writing RT_ICON 6 resource with 4264 bytes
6776 INFO: Writing RT_ICON 7 resource with 1128 bytes
6779 INFO: Copying 0 resources to EXE
6783 INFO: Emedding manifest in EXE
6784 INFO: Updating manifest in D:\Project\WA_Schema\Schema_Check\dist\schema_check_xsd_11.exe
6787 INFO: Updating resource type 24 name 1 language 0
6790 INFO: Appending PKG archive to EXE
8145 INFO: Building EXE from EXE-00.toc completed successfully.

运行 windows 应用程序 (schema_check_xsd_11.exe) 时出错:

Traceback (most recent call last):
  File "urllib\request.py", line 1474, in open_local_file
FileNotFoundError: [WinError 3] The system cannot find the path specified: 'C:\Users\test\AppData\Local\Temp\_MEI169402\xmlschema\schemas\XSD_1.0\XMLSchema.xsd'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "schema_check_xsd_11.py", line 1, in <module>
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
  File "PyInstaller\loader\pyimod03_importers.py", line 476, in exec_module
  File "xmlschema\__init__.py", line 20, in <module>
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
  File "PyInstaller\loader\pyimod03_importers.py", line 476, in exec_module
  File "xmlschema\dataobjects.py", line 27, in <module>
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
  File "PyInstaller\loader\pyimod03_importers.py", line 476, in exec_module
  File "xmlschema\validators\__init__.py", line 38, in <module>
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
  File "PyInstaller\loader\pyimod03_importers.py", line 476, in exec_module
  File "xmlschema\validators\schemas.py", line 2126, in <module>
  File "xmlschema\validators\schemas.py", line 148, in __new__
  File "xmlschema\validators\schemas.py", line 764, in create_meta_schema
  File "xmlschema\validators\schemas.py", line 357, in __init__
  File "xmlschema\resources.py", line 508, in __init__
  File "xmlschema\resources.py", line 742, in parse
  File "urllib\request.py", line 223, in urlopen
  File "urllib\request.py", line 526, in open
  File "urllib\request.py", line 544, in _open
  File "urllib\request.py", line 504, in _call_chain
  File "urllib\request.py", line 1452, in file_open
  File "urllib\request.py", line 1491, in open_local_file
urllib.error.URLError: <urlopen error [WinError 3] The system cannot find the path specified: 'C:\Users\test\AppData\Local\Temp\_MEI169402\xmlschema\schemas\XSD_1.0\XMLSchema.xsd'>

如果我从我的代码中删除 import xmlschema 并重新构建,我可以 运行 window 应用程序而不会出现任何错误。感谢您的任何建议或帮助。 安装好的xmlschema位于C:\Python36\Lib\site-packages\xmlschema\schemas\XSD_1.0.

只需将 --add-data=C:\Python36\Lib\site-packages\xmlschema\schemas\XSD_1.0;xmlschema/schemas/XSD_1.0 添加到您的 PyInstaller 命令中。

在使用 xmlschema 构建 python 脚本作为 windows 应用程序时添加以下文件。

pyinstaller --onefile --windowed ^
--add-data=C:\Python36\Lib\site-packages\xmlschema\schemas\XML\xml_minimal.xsd;xmlschema/schemas/XML ^
--add-data=C:\Python36\Lib\site-packages\xmlschema\schemas\VC\XMLSchema-versioning.xsd;xmlschema/schemas/VC ^
--add-data=C:\Python36\Lib\site-packages\xmlschema\schemas\XSD_1.1;xmlschema/schemas/XSD_1.1 ^
--add-data=C:\Python36\Lib\site-packages\xmlschema\schemas\XSI\XMLSchema-instance_minimal.xsd;xmlschema/schemas/XSI ^
--add-data=C:\Python36\Lib\site-packages\xmlschema\schemas\XSD_1.0;xmlschema/schemas/XSD_1.0 schema_check_xsd_11.py