tensorflow: With lazy_import tensorflow cannot be imported

System information

  • OS Platform and Distribution (e.g., Linux Ubuntu 16.04): Linux Ubuntu 16.04 (and gLinux)
  • Mobile device (e.g. iPhone 8, Pixel 2, Samsung Galaxy) if the issue happens on mobile device: N/A
  • TensorFlow installed from (source or binary): pip binary
  • TensorFlow version: 1.15.0.{rc1,rc2,rc3}
  • Python version: python 3.6.8
  • Installed using virtualenv? pip? conda?: anaconda
  • Bazel version (if compiling from source): -
  • GCC/Compiler version (if compiling from source): -
  • CUDA/cuDNN version: -
  • GPU model and memory: -

Describe the problem

The following works correctly:

$ python -c 'import tensorflow as tf; print(tf.__version__)'
1.15.0-rc3

In an interactive shell:

import tensorflow as tf

I get the following error: ValueError: operator __getitem__ cannot be overwritten again on class <class 'tensorflow.python.framework.ops.Tensor'>.

This happens when lazy_import is installed. I think tensorflow’s internal lazy_import has a conflict with some external library. TF 1.14 has no problem.

Though lazy_import is a third-party library, I think what it is doing is pretty non-hacky. Since this bug has appeared since 1.15.dev, we will need to have this fixed in the release version (given that 1.15 will be the last 1.x release).

Traceback:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/$HOME/.miniconda3/envs/ace-base/lib/python3.6/site-packages/tensorflow/__init__.py", line 99, in <module>
    from tensorflow_core import *
  File "/$HOME/.miniconda3/envs/ace-base/lib/python3.6/site-packages/tensorflow_core/__init__.py", line 34, in <module>
    from tensorflow._api.v1 import autograph
  File "/$HOME/.miniconda3/envs/ace-base/lib/python3.6/site-packages/tensorflow_core/_api/v1/autograph/__init__.py", line 22, in <module>
    from tensorflow._api.v1.autograph import experimental
  File "/$HOME/.miniconda3/envs/ace-base/lib/python3.6/site-packages/tensorflow_core/_api/v1/autograph/experimental/__init__.py", line 10, in <module>
    from tensorflow.python.autograph.core.converter import Feature
  File "/$HOME/.miniconda3/envs/ace-base/lib/python3.6/site-packages/tensorflow_core/python/autograph/__init__.py", line 35, in <module>
    from tensorflow.python.autograph import operators
  File "/$HOME/.miniconda3/envs/ace-base/lib/python3.6/site-packages/tensorflow_core/python/autograph/operators/__init__.py", line 40, in <module>
    from tensorflow.python.autograph.operators.control_flow import for_stmt
  File "/$HOME/.miniconda3/envs/ace-base/lib/python3.6/site-packages/tensorflow_core/python/autograph/operators/control_flow.py", line 65, in <module>
    from tensorflow.python.autograph.operators import py_builtins
  File "/$HOME/.miniconda3/envs/ace-base/lib/python3.6/site-packages/tensorflow_core/python/autograph/operators/py_builtins.py", line 28, in <module>
    from tensorflow.python.autograph.utils import py_func
  File "/$HOME/.miniconda3/envs/ace-base/lib/python3.6/site-packages/tensorflow_core/python/autograph/utils/__init__.py", line 21, in <module>
    from tensorflow.python.autograph.utils.context_managers import control_dependency_on_returns
  File "/$HOME/.miniconda3/envs/ace-base/lib/python3.6/site-packages/tensorflow_core/python/autograph/utils/context_managers.py", line 24, in <module>
    from tensorflow.python.ops import tensor_array_ops
  File "/$HOME/.miniconda3/envs/ace-base/lib/python3.6/site-packages/tensorflow_core/python/ops/tensor_array_ops.py", line 35, in <module>
    from tensorflow.python.ops import array_ops
  File "/$HOME/.miniconda3/envs/ace-base/lib/python3.6/site-packages/tensorflow_core/python/ops/array_ops.py", line 1045, in <module>
    ops.Tensor._override_operator("__getitem__", _slice_helper)
  File "/$HOME/.miniconda3/envs/ace-base/lib/python3.6/site-packages/tensorflow_core/python/framework/ops.py", line 745, in _override_operator
    _override_helper(Tensor, operator, func)
  File "/$HOME/.miniconda3/envs/ace-base/lib/python3.6/site-packages/tensorflow_core/python/framework/ops.py", line 177, in _override_helper
    (operator, clazz_object))
ValueError: operator __getitem__ cannot be overwritten again on class <class 'tensorflow.python.framework.ops.Tensor'>.

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 17 (3 by maintainers)

Most upvoted comments

Are you satisfied with the resolution of your issue? Yes No

@mihaimaruseac : It’s still a problem. I’m using tensorflow 2.2.0. Somehow you cannot lazy import numpy when tensorflow is used:

import lazy_import
np = lazy_import.lazy_module("numpy")
import tensorflow as tf

throws

Traceback (most recent call last):
  File "test2.py", line 3, in <module>
    import tensorflow as tf
  File "/home/koepke/.local/share/virtualenvs/tensorflow-0PogEeTj/lib/python3.7/site-packages/tensorflow/__init__.py", line 41, in <module>
    from tensorflow.python.tools import module_util as _module_util
  File "/home/koepke/.local/share/virtualenvs/tensorflow-0PogEeTj/lib/python3.7/site-packages/tensorflow/python/__init__.py", line 64, in <module>
    from tensorflow.python.framework.framework_lib import *  # pylint: disable=redefined-builtin
  File "/home/koepke/.local/share/virtualenvs/tensorflow-0PogEeTj/lib/python3.7/site-packages/tensorflow/python/framework/framework_lib.py", line 52, in <module>
    from tensorflow.python.framework.importer import import_graph_def
  File "/home/koepke/.local/share/virtualenvs/tensorflow-0PogEeTj/lib/python3.7/site-packages/tensorflow/python/framework/importer.py", line 28, in <module>
    from tensorflow.python.framework import function
  File "/home/koepke/.local/share/virtualenvs/tensorflow-0PogEeTj/lib/python3.7/site-packages/tensorflow/python/framework/function.py", line 36, in <module>
    from tensorflow.python.ops import array_ops
  File "/home/koepke/.local/share/virtualenvs/tensorflow-0PogEeTj/lib/python3.7/site-packages/tensorflow/python/ops/array_ops.py", line 1428, in <module>
    _NON_AUTOPACKABLE_TYPES = set(np.core.numerictypes.ScalarType)
AttributeError: module 'numpy.core' has no attribute 'numerictypes'

Do note that

import lazy_import
np = lazy_import.lazy_module("numpy")
print(np.core.numerictypes)

and

import lazy_import
np = lazy_import.lazy_module("numpy")
print(np.core.numerictypes.ScalarType)

both work as expected.

I tired all of the code above and they are indeed working as expected (printing version without the problem). Sorry about confusing. I found that it was actually from another lazy_import of numpy, specifically, when numpy is yet to be fully loaded:

import lazy_import
np = lazy_import.lazy_module("numpy")
import tensorflow as tf
# gives ValueError: operator __getitem__ cannot be overwritten again on class <class 'tensorflow.python.framework.ops.Tensor'>.

I can reproduce this on 3 different environments including glinux and macOS, both anaconda and brew python.

Not sure tensorflow is to blame or lazy_import is, but let me now look into why it happens.