Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 590
Bill B Avatar asked Feb 24 '20 21:02

Bill B


People also ask

How do I import a module into PyInstaller?

Find a module name, then keep clicking the “imported by” links until you find the top-level import that causes that module to be included. If you specify --log-level=DEBUG to the pyinstaller command, PyInstaller additionally generates a GraphViz input file representing the dependency graph.

Does PyInstaller work with libraries?

PyInstaller does not include libraries that should exist in any installation of this OS. For example in GNU/Linux, it does not bundle any file from /lib or /usr/lib , assuming these will be found in every system.

How do I fix PyInstaller not working?

To Solve 'pyinstaller' is not recognized as an internal or external command operable program or batch file Error You just need to add python script in your path to solve this error.


1 Answers

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 *

like image 80
Legorooj Avatar answered Oct 17 '22 02:10

Legorooj