Repository: pyinstaller/pyinstaller

Issue:
Created exe from .py using pyinstaller has issues with tensorflow path 
Using pyinstaller I have created an exe from python script. The script has dependency on tensorflow. The package is created perfectly. The issue is when I run the exe.

Python 3.6.5 Pyinstaller 3.3.1 Build platform : Win 8.1 64bit Running platform Win 7 64bit

C:\Program Files\test\FaceDetectionPackageBeta1\dist\FaceDetection>FaceDetection.exe
Using TensorFlow backend.
Traceback (most recent call last):
  File "site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 14,
 in swig_import_helper
  File "importlib\__init__.py", line 126, in import_module
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 953, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'tensorflow.python._pywrap_tensorflow_inter
nal'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "site-packages\tensorflow\python\pywrap_tensorflow.py", line 58, in <modu
le>
  File "c:\program files\python36\lib\site-packages\PyInstaller\loader\pyimod03_
importers.py", line 631, in exec_module
  File "site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 17,
 in <module>
  File "site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 16,
 in swig_import_helper
  File "importlib\__init__.py", line 126, in import_module
  File "c:\program files\python36\lib\site-packages\PyInstaller\loader\pyimod03_
importers.py", line 714, in load_module
ImportError: DLL load failed with error code -1073741795

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "TELFaceDetection.py", line 12, in <module>
  File "c:\program files\python36\lib\site-packages\PyInstaller\loader\pyimod03_
importers.py", line 631, in exec_module
  File "site-packages\keras\__init__.py", line 3, in <module>
  File "c:\program files\python36\lib\site-packages\PyInstaller\loader\pyimod03_
importers.py", line 631, in exec_module
  File "site-packages\keras\utils\__init__.py", line 6, in <module>
  File "c:\program files\python36\lib\site-packages\PyInstaller\loader\pyimod03_
importers.py", line 631, in exec_module
  File "site-packages\keras\utils\conv_utils.py", line 9, in <module>
  File "c:\program files\python36\lib\site-packages\PyInstaller\loader\pyimod03_
importers.py", line 631, in exec_module
  File "site-packages\keras\backend\__init__.py", line 89, in <module>
  File "c:\program files\python36\lib\site-packages\PyInstaller\loader\pyimod03_
importers.py", line 631, in exec_module
  File "site-packages\keras\backend\tensorflow_backend.py", line 5, in <module>
  File "c:\program files\python36\lib\site-packages\PyInstaller\loader\pyimod03_
importers.py", line 631, in exec_module
  File "site-packages\tensorflow\__init__.py", line 24, in <module>
  File "c:\program files\python36\lib\site-packages\PyInstaller\loader\pyimod03_
importers.py", line 631, in exec_module
  File "site-packages\tensorflow\python\__init__.py", line 49, in <module>
  File "c:\program files\python36\lib\site-packages\PyInstaller\loader\pyimod03_
importers.py", line 631, in exec_module
  File "site-packages\tensorflow\python\pywrap_tensorflow.py", line 74, in <modu
le>
ImportError: Traceback (most recent call last):
  File "site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 14,
 in swig_import_helper
  File "importlib\__init__.py", line 126, in import_module
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 953, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'tensorflow.python._pywrap_tensorflow_inter
nal'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "site-packages\tensorflow\python\pywrap_tensorflow.py", line 58, in <modu
le>
  File "c:\program files\python36\lib\site-packages\PyInstaller\loader\pyimod03_
importers.py", line 631, in exec_module
  File "site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 17,
 in <module>
  File "site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 16,
 in swig_import_helper
  File "importlib\__init__.py", line 126, in import_module
  File "c:\program files\python36\lib\site-packages\PyInstaller\loader\pyimod03_
importers.py", line 714, in load_module
ImportError: DLL load failed with error code -1073741795


Failed to load the native TensorFlow runtime.

See https://www.tensorflow.org/install/install_sources#common_installation_probl
ems

for some common reasons and solutions.  Include the entire stack trace
above this error message when asking for help.
[4988] Failed to execute script TELFaceDetection
Below is the spec file

# -*- mode: python -*-

block_cipher = None


a = Analysis(['FaceDetection.py'],
             pathex=['D:\\FaceDetectionSetup\\FaceDetectioPackageSource'],
             binaries=[],
             datas=[],
             hiddenimports=[],
             hookspath=[],
             runtime_hooks=[],
             excludes=[],
             win_no_prefer_redirects=False,
             win_private_assemblies=False,
             cipher=block_cipher)
pyz = PYZ(a.pure, a.zipped_data,
             cipher=block_cipher)
exe = EXE(pyz,
          a.scripts,
          exclude_binaries=True,
          name='FaceDetection',
          debug=False,
          strip=False,
          upx=True,
          console=True )
coll = COLLECT(exe,
               a.binaries,
               a.zipfiles,
               a.datas,
               strip=False,
               upx=True,
               name='FaceDetection')
I have tried the solution in link, but that does not work either. when I execute the exe, No module named tensorflow.python.framework.load_library error is received.

How do I set the path so that relative path is picked. The same worked without any issues on Win 7 64bit desktop system.So working on one system and not on the other.Any help appreciated.

Thanks -Megha


Discussion:
> Build platform Win 8.1 64bit

> Running platform Win 7 64bit

I don't think this can work, see pyinstaller's [docs](https://pythonhosted.org/PyInstaller/operating-mode.html#what-pyinstaller-does-and-how-it-does-it)

> The output of PyInstaller is specific to the active operating system and the active version of Python

If you want to run on Windows 7, you need to build on Windows 7.

That being said, tensorflow is a bit of a special case. See my ticket #3754



Resolving patch:
diff --git a/PyInstaller/hooks/hook-astor.py b/PyInstaller/hooks/hook-astor.py
new file mode 100644
--- /dev/null
+++ b/PyInstaller/hooks/hook-astor.py
@@ -0,0 +1,14 @@
+#-----------------------------------------------------------------------------
+# Copyright (c) 2013-2020, PyInstaller Development Team.
+#
+# Distributed under the terms of the GNU General Public License (version 2
+# or later) with exception for distributing the bootloader.
+#
+# The full license is in the file COPYING.txt, distributed with this software.
+#
+# SPDX-License-Identifier: (GPL-2.0-or-later WITH Bootloader-exception)
+#-----------------------------------------------------------------------------
+
+from PyInstaller.utils.hooks import collect_data_files
+
+datas = collect_data_files('astor')
diff --git a/PyInstaller/hooks/hook-tensorflow.py b/PyInstaller/hooks/hook-tensorflow.py
new file mode 100644
--- /dev/null
+++ b/PyInstaller/hooks/hook-tensorflow.py
@@ -0,0 +1,12 @@
+#-----------------------------------------------------------------------------
+# Copyright (c) 2013-2020, PyInstaller Development Team.
+#
+# Distributed under the terms of the GNU General Public License (version 2
+# or later) with exception for distributing the bootloader.
+#
+# The full license is in the file COPYING.txt, distributed with this software.
+#
+# SPDX-License-Identifier: (GPL-2.0-or-later WITH Bootloader-exception)
+#-----------------------------------------------------------------------------
+
+hiddenimports = ['tensorflow_core']
diff --git a/PyInstaller/hooks/hook-tensorflow_core.python.py b/PyInstaller/hooks/hook-tensorflow_core.python.py
new file mode 100644
--- /dev/null
+++ b/PyInstaller/hooks/hook-tensorflow_core.python.py
@@ -0,0 +1,15 @@
+#-----------------------------------------------------------------------------
+# Copyright (c) 2013-2020, PyInstaller Development Team.
+#
+# Distributed under the terms of the GNU General Public License (version 2
+# or later) with exception for distributing the bootloader.
+#
+# The full license is in the file COPYING.txt, distributed with this software.
+#
+# SPDX-License-Identifier: (GPL-2.0-or-later WITH Bootloader-exception)
+#-----------------------------------------------------------------------------
+
+from PyInstaller.utils.hooks import collect_submodules, collect_data_files
+
+hiddenimports = collect_submodules('tensorflow_core')
+datas = collect_data_files('tensorflow_core')


Test patch:
diff --git a/tests/functional/test_libraries.py b/tests/functional/test_libraries.py
--- a/tests/functional/test_libraries.py
+++ b/tests/functional/test_libraries.py
@@ -75,6 +75,15 @@ def test_enchant(pyi_builder):
     pyi_builder.test_script('pyi_lib_enchant.py')
 
 
+@importorskip('tensorflow')
+def test_tensorflow(pyi_builder):
+    pyi_builder.test_source(
+        """
+        from tensorflow import *
+        """
+    )
+
+
 @importorskip('gevent')
 def test_gevent(pyi_builder):
     pyi_builder.test_source(