tensorflow: Can't import Layer from tensorflow.python.keras.engine.base_layer

I’m getting an error that says from tensorflow.python.keras.engine.base_layer import Layer while importing tensorflow in my app.

System information

  • Have I written custom code (as opposed to using a stock example script provided in TensorFlow): Yes
  • OS Platform and Distribution (e.g., Linux Ubuntu 16.04): MacOS
  • TensorFlow installed from (source or binary): binary
  • TensorFlow version (use command below): 1.15.0
  • Python version: 3.7

Describe the current behavior On packaging an app using tensorflow with py2app, I get the following error while running the app:

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)

Describe the expected behavior The packaged app should work as expected.

Code to reproduce the issue Provide a reproducible test case that is the bare minimum necessary to generate the problem.

import tensorflow as tf

self.model_path = r"{}".format(
            pathlib.Path(os.getcwd()).joinpath('model'))
        self.session = tf.Session(graph=tf.Graph())
        tf.saved_model.loader.load(self.session, ['serve'], self.model_path)

here’s the setup.py that I’ve used with py2app:

"""
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']
)

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 5
  • Comments: 22 (2 by maintainers)

Most upvoted comments

I am using the latest version of tensorflow and still have such problem. My tensorflow is installed using anaconda

How did you solve the problem? I keep getting the same error

I also have this problem.How do you solve this problem in last?

This issue still exists.

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you.

I also have same issue on Windows 10 Enterprise after packaging my python project using Pyinstaller. Tf version=1.15

This issue still persists.

Regards Guramritpal Saggu

On Tue, 21 Apr 2020, 1:39 am tensorflow-butler[bot], < notifications@github.com> wrote:

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/tensorflow/tensorflow/issues/36814#issuecomment-616780962, or unsubscribe https://github.com/notifications/unsubscribe-auth/AIUWZEHXBZNJIBMUAKIBNVDRNSTVZANCNFSM4KWNXHHA .