使用模块将python编译为Windows可执行文件失败

时间:2020-01-08 19:48:12

标签: python pyinstaller

我正在尝试将python代码编译为Windows可执行文件,但是出现问题。我尝试了“ pyinstaller”,但是每次我将python代码编译为Windows可执行文件并运行它时,都会出现一个异常,提示“没有名为XXXX的模块”,我正在使用face_recognition,numpy和opencv,任何想法如何解决此问题?

!> pyinstaller --hidden-import cv2 --hidden-import face_recognition --hidden-import numpy main.py
2891 INFO: PyInstaller: 3.5
2891 INFO: Python: 2.7.17
2891 INFO: Platform: Windows-10-10.0.18362
2891 INFO: wrote C:\Users\Arman\Desktop\sysFace\main.spec
3008 INFO: UPX is not available.
3017 INFO: Extending PYTHONPATH with paths
['C:\\Users\\Arman\\Desktop\\sysFace', 'C:\\Users\\Arman\\Desktop\\sysFace']
3017 INFO: checking Analysis
3018 INFO: Building Analysis because Analysis-00.toc is non existent
3019 INFO: Initializing module dependency graph...
3054 INFO: Initializing module graph hooks...
3133 INFO: Analyzing hidden import 'cv2'
3133 ERROR: Hidden import 'cv2' not found
3133 INFO: Analyzing hidden import 'face_recognition'
3133 ERROR: Hidden import 'face_recognition' not found
3133 INFO: Analyzing hidden import 'numpy'
3133 ERROR: Hidden import 'numpy' not found
3180 INFO: running Analysis Analysis-00.toc
3305 INFO: Adding Microsoft.VC90.CRT to dependent assemblies of final executable
  required by c:\python27\python.exe
10000 INFO: Found C:\WINDOWS\WinSxS\Manifests\amd64_policy.9.0.microsoft.vc90.crt_1fc8b3b9a1e18e3b_9.0.30411.0_none_37e824fec5a2ba27.manifest
10082 INFO: Found C:\WINDOWS\WinSxS\Manifests\amd64_policy.9.0.microsoft.vc90.crt_1fc8b3b9a1e18e3b_9.0.30729.1_none_3da38fdebd0e6822.manifest
10093 INFO: Found C:\WINDOWS\WinSxS\Manifests\amd64_policy.9.0.microsoft.vc90.crt_1fc8b3b9a1e18e3b_9.0.30729.4148_none_acd0e4ffe1daef0a.manifest
10118 INFO: Found C:\WINDOWS\WinSxS\Manifests\amd64_policy.9.0.microsoft.vc90.crt_1fc8b3b9a1e18e3b_9.0.30729.9619_none_accdd623e1ddaffe.manifest
10227 INFO: Searching for assembly amd64_Microsoft.VC90.CRT_1fc8b3b9a1e18e3b_9.0.30729.9619_none ...
10227 INFO: Found manifest C:\WINDOWS\WinSxS\Manifests\amd64_microsoft.vc90.crt_1fc8b3b9a1e18e3b_9.0.30729.9619_none_08e065a3a84109b0.manifest
10243 INFO: Searching for file msvcr90.dll
10243 INFO: Found file C:\WINDOWS\WinSxS\amd64_microsoft.vc90.crt_1fc8b3b9a1e18e3b_9.0.30729.9619_none_08e065a3a84109b0\msvcr90.dll
10243 INFO: Searching for file msvcp90.dll
10243 INFO: Found file C:\WINDOWS\WinSxS\amd64_microsoft.vc90.crt_1fc8b3b9a1e18e3b_9.0.30729.9619_none_08e065a3a84109b0\msvcp90.dll
10243 INFO: Searching for file msvcm90.dll
10243 INFO: Found file C:\WINDOWS\WinSxS\amd64_microsoft.vc90.crt_1fc8b3b9a1e18e3b_9.0.30729.9619_none_08e065a3a84109b0\msvcm90.dll
10336 INFO: Found C:\WINDOWS\WinSxS\Manifests\amd64_policy.9.0.microsoft.vc90.crt_1fc8b3b9a1e18e3b_9.0.30411.0_none_37e824fec5a2ba27.manifest
10336 INFO: Found C:\WINDOWS\WinSxS\Manifests\amd64_policy.9.0.microsoft.vc90.crt_1fc8b3b9a1e18e3b_9.0.30729.1_none_3da38fdebd0e6822.manifest
10336 INFO: Found C:\WINDOWS\WinSxS\Manifests\amd64_policy.9.0.microsoft.vc90.crt_1fc8b3b9a1e18e3b_9.0.30729.4148_none_acd0e4ffe1daef0a.manifest
10336 INFO: Found C:\WINDOWS\WinSxS\Manifests\amd64_policy.9.0.microsoft.vc90.crt_1fc8b3b9a1e18e3b_9.0.30729.9619_none_accdd623e1ddaffe.manifest
10336 INFO: Adding redirect Microsoft.VC90.CRT version (9, 0, 21022, 8) -> (9, 0, 30729, 9619)
10540 INFO: Caching module hooks...
10728 INFO: Analyzing C:\Users\Arman\Desktop\sysFace\main.py
12764 INFO: Loading module hooks...
12764 INFO: Loading module hook "hook-encodings.py"...
14608 INFO: Looking for ctypes DLLs
14608 INFO: Analyzing run-time hooks ...
14625 INFO: Looking for dynamic libraries
14718 INFO: Looking for eggs
14718 INFO: Using Python library C:\WINDOWS\system32\python27.dll
14733 INFO: Found binding redirects:
[BindingRedirect(name=u'Microsoft.VC90.CRT', language=None, arch=u'amd64', oldVersion=(9, 0, 21022, 8), newVersion=(9, 0, 30729, 9619), publicKeyToken=u'1fc8b3b9a1e18e3b')]
14733 INFO: Warnings written to C:\Users\Arman\Desktop\sysFace\build\main\warn-main.txt
14750 INFO: Graph cross-reference written to C:\Users\Arman\Desktop\sysFace\build\main\xref-main.html
14843 INFO: checking PYZ
14843 INFO: Building PYZ because PYZ-00.toc is non existent
14843 INFO: Building PYZ (ZlibArchive) C:\Users\Arman\Desktop\sysFace\build\main\PYZ-00.pyz
15096 INFO: Building PYZ (ZlibArchive) C:\Users\Arman\Desktop\sysFace\build\main\PYZ-00.pyz completed successfully.
15143 INFO: checking PKG
15143 INFO: Building PKG because PKG-00.toc is non existent
15143 INFO: Building PKG (CArchive) PKG-00.pkg
15206 INFO: Building PKG (CArchive) PKG-00.pkg completed successfully.
15206 INFO: Bootloader c:\python27\lib\site-packages\PyInstaller\bootloader\Windows-64bit\run.exe
15206 INFO: checking EXE
15206 INFO: Building EXE because EXE-00.toc is non existent
15206 INFO: Building EXE from EXE-00.toc
15237 INFO: Appending archive to EXE C:\Users\Arman\Desktop\sysFace\build\main\main.exe
15487 INFO: Building EXE from EXE-00.toc completed successfully.
15503 INFO: checking COLLECT
15503 INFO: Building COLLECT because COLLECT-00.toc is non existent
15503 INFO: Building COLLECT COLLECT-00.toc
15675 INFO: Redirecting Microsoft.VC90.CRT version (9, 0, 21022, 8) -> (9, 0, 30729, 9619)
16583 INFO: Building COLLECT COLLECT-00.toc completed successfully.

操作系统:Windows 10 Pro x64 IDE:Visual Studio代码 的Python:python v3.8 模块:face_recognition,cv2,numpy pyinstaller:pyinstaller 3.5 点:点v19.2.3

在更新模块并将pip设置为19.3.1之后,我尝试了pyinstaller --hidden-import cv2 --hidden-import face_recognition --hidden-import numpy main.py,并且在启动应用程序时出现了以下异常情况:

  File "main.py", line 1, in <module>
    import face_recognition
  File "c:\users\arman\appdata\local\temp\pip-install-jtbivm\pyinstaller\PyInstaller\loader\pyimod03_importers.py", line 395, in load_module
  File "site-packages\face_recognition\__init__.py", line 7, in <module>
  File "c:\users\arman\appdata\local\temp\pip-install-jtbivm\pyinstaller\PyInstaller\loader\pyimod03_importers.py", line 395, in load_module
  File "site-packages\face_recognition\api.py", line 17, in <module>
RuntimeError: Unable to open C:\Users\Arman\Desktop\sysFace\dist\main\face_recognition_models\models\shape_predictor_68_face_landmarks.dat
[11588] Failed to execute script main```

1 个答案:

答案 0 :(得分:0)

更新pip和您的模块将解决此问题,但是在导入“ face_recognition”模块时遇到了另一个问题。 Pyinstaller无法在face_recognition的文件夹中加载.dat文件,因此,如果发生这种情况,请执行以下步骤:

1)打开CMD 2)运行以下命令pyi-makespec urpythonscript.py 3)使用文本编辑器打开规范文件,然后添加.dat文件的位置。 在datas=[] 4)hiddenimports=[]中添加您的模块名称。 5)返回CMD并运行pyinstaller urpythonscript.py 6)/dist/urpythonscript/urpythonscript.exe中启动可执行文件 7)。您完成了。

也感谢Python Discord社区的帮助。