ronaldoussoren / py2app

py2app is a Python setuptools command which will allow you to make standalone Mac OS X application bundles and plugins from Python scripts.
Other
350 stars 35 forks source link

Error while packaging tensorflow 1.15 #283

Closed harshithdwivedi closed 3 years ago

harshithdwivedi commented 4 years ago

Hi again! While packaging tensorflow 1.15 with py2app in a project of mine, I'm getting an error that's concerning Keras.

This is what the error looks like:

File "/Users/harshitdwivedi/Desktop/aftershootscripts/dist/aftershoot.app/Contents/Resources/lib/python3.7/tensorflow_core/__init__.py", line 36, in <module>
    from tensorflow._api.v1 import compat
  File "/Users/harshitdwivedi/Desktop/aftershootscripts/dist/aftershoot.app/Contents/Resources/lib/python3.7/tensorflow_core/_api/v1/compat/__init__.py", line 23, in <module>
    from tensorflow._api.v1.compat import v1
  File "/Users/harshitdwivedi/Desktop/aftershootscripts/dist/aftershoot.app/Contents/Resources/lib/python3.7/tensorflow_core/_api/v1/compat/v1/__init__.py", line 40, in <module>
    from tensorflow._api.v1.compat.v1 import experimental
  File "/Users/harshitdwivedi/Desktop/aftershootscripts/dist/aftershoot.app/Contents/Resources/lib/python3.7/tensorflow_core/_api/v1/compat/v1/experimental/__init__.py", line 11, in <module>
    from tensorflow.python.ops.control_flow_v2_toggles import output_all_intermediates
  File "/Users/harshitdwivedi/Desktop/aftershootscripts/dist/aftershoot.app/Contents/Resources/lib/python3.7/tensorflow_core/python/ops/control_flow_v2_toggles.py", line 24, in <module>
    from tensorflow.python.ops import control_flow_util_v2
  File "/Users/harshitdwivedi/Desktop/aftershootscripts/dist/aftershoot.app/Contents/Resources/lib/python3.7/tensorflow_core/python/ops/control_flow_util_v2.py", line 28, in <module>
    from tensorflow.python.keras.engine import base_layer_utils
  File "/Users/harshitdwivedi/Desktop/aftershootscripts/dist/aftershoot.app/Contents/Resources/lib/python3.7/tensorflow_core/python/keras/__init__.py", line 27, in <module>
    from tensorflow.python.keras import applications
  File "/Users/harshitdwivedi/Desktop/aftershootscripts/dist/aftershoot.app/Contents/Resources/lib/python3.7/tensorflow_core/python/keras/applications/__init__.py", line 25, in <module>
    from tensorflow.python.keras import engine
  File "/Users/harshitdwivedi/Desktop/aftershootscripts/dist/aftershoot.app/Contents/Resources/lib/python3.7/tensorflow_core/python/keras/engine/__init__.py", line 23, in <module>
    from tensorflow.python.keras.engine.base_layer import Layer
  File "/Users/harshitdwivedi/Desktop/aftershootscripts/dist/aftershoot.app/Contents/Resources/lib/python3.7/tensorflow_core/python/keras/engine/base_layer.py", line 58, in <module>
    from tensorflow.python.keras.saving.saved_model import save as saved_model
  File "/Users/harshitdwivedi/Desktop/aftershootscripts/dist/aftershoot.app/Contents/Resources/lib/python3.7/tensorflow_core/python/keras/saving/saved_model/save.py", line 30, in <module>
    from tensorflow.python.keras.saving import saving_utils
  File "/Users/harshitdwivedi/Desktop/aftershootscripts/dist/aftershoot.app/Contents/Resources/lib/python3.7/tensorflow_core/python/keras/saving/__init__.py", line 33, in <module>
    from tensorflow.python.keras.saving.saved_model_experimental import export_saved_model
  File "/Users/harshitdwivedi/Desktop/aftershootscripts/dist/aftershoot.app/Contents/Resources/lib/python3.7/tensorflow_core/python/keras/saving/saved_model_experimental.py", line 30, in <module>
    from tensorflow.python.keras.utils import mode_keys
  File "/Users/harshitdwivedi/Desktop/aftershootscripts/dist/aftershoot.app/Contents/Resources/lib/python3.7/tensorflow_core/python/keras/utils/__init__.py", line 38, in <module>
    from tensorflow.python.keras.utils.multi_gpu_utils import multi_gpu_model
  File "/Users/harshitdwivedi/Desktop/aftershootscripts/dist/aftershoot.app/Contents/Resources/lib/python3.7/tensorflow_core/python/keras/utils/multi_gpu_utils.py", line 22, in <module>
    from tensorflow.python.keras.engine.training import Model
  File "/Users/harshitdwivedi/Desktop/aftershootscripts/dist/aftershoot.app/Contents/Resources/lib/python3.7/tensorflow_core/python/keras/engine/training.py", line 42, in <module>
    from tensorflow.python.keras import metrics as metrics_module
  File "/Users/harshitdwivedi/Desktop/aftershootscripts/dist/aftershoot.app/Contents/Resources/lib/python3.7/tensorflow_core/python/keras/metrics.py", line 34, in <module>
    from tensorflow.python.keras.engine.base_layer import Layer
ImportError: cannot import name 'Layer' from 'tensorflow.python.keras.engine.base_layer' (/Users/harshitdwivedi/Desktop/aftershootscripts/dist/aftershoot.app/Contents/Resources/lib/python3.7/tensorflow_core/python/keras/engine/base_layer.py)

Here's my setup.py for the project:

"""
This is a setup.py script generated by py2applet

Usage:
    python setup.py py2app
"""

from setuptools import setup

APP = ['aftershoot.py']
DATA_FILES = []
OPTIONS = {
        'packages' : ['PySide2', 'shiboken2', 'tensorflow_core', 'astor'],
        'resources' : ['exposure/']
        # 'optimize' : 1,
        }

setup(
    app=APP,
    data_files=DATA_FILES,
    options={'py2app': OPTIONS},
    setup_requires=['py2app']
)

A minimal piece of code that can help reproduce this issue:

import tensorflow as tf

# create a blank file and call it `saved_model.py` and place it inside `folder`
model_path = 'folder'
session = tf.Session(graph=tf.Graph())
tf.saved_model.loader.load(session, ['serve'], model_path)

System configuration:

Python Version: 3.7.6 Py2App version: 0.21

harshithdwivedi commented 4 years ago

Hi @ronaldoussoren can you look into this? A built in recipe for Tensorflow would be great, I believe that issue #282 is linked to this.

ronaldoussoren commented 3 years ago

I've added a minimal recipe to the repository (and expect to push out a release before the end of the year).

The recipe is at this point very minimal and just includes the entire TensorFlow package into the generated app bundle. Given the size of that package it may be possible to improve on this by leaving out part of the package by default. But the first step is ensuring that using TensorFlow works.

ronaldoussoren commented 3 years ago

Closing the issue because automatically including all of tensorflow fixes the packaging issue.