Search code examples
pythontensorflowpyinstaller

pyinstaller ModuleNotFoundError


I have built a python script using tensorflow and I am now trying to convert it to an .exe file, but have ran into a problem. After using pyinstaller and running the program from the command prompt I get the following error:

File "site-packages\tensorflow_core\python\pywrap_tensorflow.py", line 25, in <module> ModuleNotFoundError: No module named 'tensorflow.python.platform'

I have tried --hidden-import tensorflow.python.platform but it seems to have fixed nothing. (The program runs just fine in the interpreter) Your help would be greatly appreciated.


Solution

  • EDIT: The latest versions of PyInstaller (4.0+) now include support for tensorflow out of the box.

    Create a directory structure like this:

    - main.py  # Your code goes here - don't bother actually naming you file this
    - hooks
      - hook-tensorflow.py
    

    Copy the following into hook-tensorflow.py:

    from PyInstaller.utils.hooks import collect_all
    
    
    def hook(hook_api):
        packages = [
            'tensorflow',
            'tensorflow_core',
            'astor'
        ]
        for package in packages:
            datas, binaries, hiddenimports = collect_all(package)
            hook_api.add_datas(datas)
            hook_api.add_binaries(binaries)
            hook_api.add_imports(*hiddenimports)
    

    Then, when compiling, add the command line option --additional-hooks-dir=hooks.

    If you come across more not found errors, simply add the full import name into the packages list.

    PS - for me, main.py was simply from tensorflow import *