-1

I'm trying to compile my python code to windows executable but I'm having issues. I tried "pyinstaller" but everytime I compiling my python code to windows executable and run it i get an exception saying that "no module named XXXX" I'm using face_recognition, numpy and opencv, any Idea how to fix this?

!> 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.

OS: Windows 10 Pro x64 IDE: Visual Studio Code Python: python v3.8 Modules: face_recognition, cv2, numpy Pyinstaller: pyinstaller 3.5 Pip: pip v19.2.3

After updating my modules and pip to 19.3.1 I tried pyinstaller --hidden-import cv2 --hidden-import face_recognition --hidden-import numpy main.py and when I launched the application it gave me this exception:

  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```
Nedd
  • 13
  • 5
  • Have you tried the answers to this question: https://stackoverflow.com/questions/25733467/no-module-named-when-using-pyinstaller ? – Nathan Jan 08 '20 at 19:53
  • The first one didin't work. Let me try the second solution. – Nedd Jan 08 '20 at 19:59
  • Also when I use the ```--hidden-import XXXX``` one In terminal is says "ERROR: Hidden Module XXX not found". – Nedd Jan 08 '20 at 20:03
  • This is a bit difficult to help you with. Do you have an example piece of code that doesn't compile? – Nathan Jan 08 '20 at 20:05
  • The .spec one didn't work... – Nedd Jan 08 '20 at 20:07
  • I'm voting to close this, there are a few different questions in here, and certainly not enough information to fix the issue. – AMC Jan 08 '20 at 20:19
  • ```import cv2 import os cap = cv2.VideoCapture(0) while True: ret, frame = cap.read() cv2.imshow("Vid", frame) if cv2.waitKey(20) == 27: break cap.release() cv2.destroyAllWindows()``` An example of code that doesn't work when compiled. – Nedd Jan 08 '20 at 20:23
  • 1
    Well there is enough info, If you don't have solution to fix this problem that doesn't mean that there is no solution for it. :) – Nedd Jan 08 '20 at 20:40
  • Also there is one question not few, read it again. – Nedd Jan 08 '20 at 20:41
  • @Nedd **1.** [Edit] your question and move your example code from comment to your question. **2.** Show your attempt about the `.spec` option. **3.** Read [using-opencv-with-pyinstaller](https://stackoverflow.com/questions/55175419/using-opencv-with-pyinstaller) – stovfl Jan 08 '20 at 20:50
  • _Well there is enough info, If you don't have solution to fix this problem that doesn't mean that there is no solution for it. :)_ Is that so? How can I reproduce this? Odds are the issue is with your Python environment and the packages, which is extremely specific and difficult to reproduce. – AMC Jan 08 '20 at 22:42

1 Answers1

0

Updating pip and your modules will fix the problem, but I faced another problem while importing the "face_recognition" module. Pyinstaller was failing to load a .dat file in face_recognition's folder, so if this happens to you do the following steps:

1) Open CMD 2) Run the following command pyi-makespec urpythonscript.py. 3) Open the spec file with text editor and add the .dat file's location. in datas=[] 4) Add you're module names in hiddenimports=[]. 5) Go back to CMD and run pyinstaller urpythonscript.py. 6) Launch the executable file in /dist/urpythonscript/urpythonscript.exe 7) You're done.

Also thanks to Python Discord Community for the help.

Nedd
  • 13
  • 5