7

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.

Bill B
  • 73
  • 3
  • 7
  • Write a hook: https://pyinstaller.readthedocs.io/en/stable/hooks.html. – Legorooj Feb 25 '20 at 05:06
  • I'm sorry but I'm a bit of a newbie when it comes to this stuff. I read the docs but didn't fully understand how to write the hook and how it differs from a hidden import. If you someone could guide me further that would be great. – Bill B Feb 25 '20 at 17:20
  • I'll put an answer/tutorial on. – Legorooj Feb 26 '20 at 03:44

1 Answers1

20

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 *

Legorooj
  • 2,646
  • 2
  • 15
  • 35