Error: Root: Internal Python Error in the Inspect Module While Installing Tensorflow

Error: Root: Internal Python Error in the Inspect Module While Installing Tensorflow

I have installed TensorFlow 2.0 with CUDA 10.1, cuDNN SDK = 7.6 and all the different libraries such as pandas, sci-kit learn, Keras, NumPy, Gensim etc. This error is probably due to new TensorFlow version. When I try to confirm it by running this confirmation code. I am getting an error. Kindly help.

import sys

import tensorflow.keras
import pandas as pd
import sklearn as sk
import tensorflow as tf

print(f"Tensor Flow Version: {tf.__version__}")
print(f"Keras Version: {tensorflow.keras.__version__}")
print()
print(f"Python {sys.version}")
print(f"Pandas {pd.__version__}")
print(f"Scikit-Learn {sk.__version__}")
print("GPU is", "available" if tf.test.is_gpu_available() else "NOT AVAILABLE")

Error I am getting:

WARNING:tensorflow:From C:\Users\Abdul\.conda\envs\tensorflow\lib\site-packages\tensorflow_core\python\ops\distributions\distribution.py:265: ReparameterizationType.__init__ (from tensorflow.python.ops.distributions.distribution) is deprecated and will be removed after 2019-01-01.
Instructions for updating:
The TensorFlow Distributions library has moved to TensorFlow Probability (). You should update all references to use `tfp.distributions` instead of `tf.distributions`.
WARNING:tensorflow:From C:\Users\Abdul\.conda\envs\tensorflow\lib\site-packages\tensorflow_core\python\ops\distributions\bernoulli.py:169: RegisterKL.__init__ (from tensorflow.python.ops.distributions.kullback_leibler) is deprecated and will be removed after 2019-01-01.
Instructions for updating:
The TensorFlow Distributions library has moved to TensorFlow Probability (). You should update all references to use `tfp.distributions` instead of `tf.distributions`.
ERROR:root:Internal Python error in the inspect module.
Below is the traceback from this internal error.

Traceback (most recent call last):
  File "C:\Users\Abdul\.conda\envs\tensorflow\lib\site-packages\IPython\core\interactiveshell.py", line 3331, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-1-0b3eb45acbe5>", line 3, in <module>
    import tensorflow.keras
  File "C:\Users\Abdul\.conda\envs\tensorflow\lib\site-packages\tensorflow\__init__.py", line 98, in <module>
    from tensorflow_core import *
  File "C:\Users\Abdul\.conda\envs\tensorflow\lib\site-packages\tensorflow_core\__init__.py", line 40, in <module>
    from tensorflow.python.tools import module_util as _module_util
ModuleNotFoundError: No module named 'tensorflow.python.tools'; 'tensorflow.python' is not a package

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\Abdul\.conda\envs\tensorflow\lib\site-packages\IPython\core\interactiveshell.py", line 2044, in showtraceback
    stb = value._render_traceback_()
AttributeError: 'ModuleNotFoundError' object has no attribute '_render_traceback_'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\Abdul\.conda\envs\tensorflow\lib\site-packages\IPython\core\ultratb.py", line 1151, in get_records
    return _fixed_getinnerframes(etb, number_of_lines_of_context, tb_offset)
  File "C:\Users\Abdul\.conda\envs\tensorflow\lib\site-packages\IPython\core\ultratb.py", line 319, in wrapped
    return f(*args, **kwargs)
  File "C:\Users\Abdul\.conda\envs\tensorflow\lib\site-packages\IPython\core\ultratb.py", line 353, in _fixed_getinnerframes
    records = fix_frame_records_filenames(inspect.getinnerframes(etb, context))
  File "C:\Users\Abdul\.conda\envs\tensorflow\lib\inspect.py", line 1502, in getinnerframes
    frameinfo = (tb.tb_frame,) + getframeinfo(tb, context)
  File "C:\Users\Abdul\.conda\envs\tensorflow\lib\inspect.py", line 1460, in getframeinfo
    filename = getsourcefile(frame) or getfile(frame)
  File "C:\Users\Abdul\.conda\envs\tensorflow\lib\inspect.py", line 696, in getsourcefile
    if getattr(getmodule(object, filename), '__loader__', None) is not None:
  File "C:\Users\Abdul\.conda\envs\tensorflow\lib\inspect.py", line 733, in getmodule
    if ismodule(module) and hasattr(module, '__file__'):
  File "C:\Users\Abdul\.conda\envs\tensorflow\lib\site-packages\tensorflow\__init__.py", line 50, in __getattr__
    module = self._load()
  File "C:\Users\Abdul\.conda\envs\tensorflow\lib\site-packages\tensorflow\__init__.py", line 44, in _load
    module = _importlib.import_module(self.__name__)
  File "C:\Users\Abdul\.conda\envs\tensorflow\lib\importlib\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 953, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 728, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "C:\Users\Abdul\.conda\envs\tensorflow\lib\site-packages\tensorflow_core\__init__.py", line 45, in <module>
    from . _api.v2 import compat
  File "C:\Users\Abdul\.conda\envs\tensorflow\lib\site-packages\tensorflow_core\_api\v2\compat\__init__.py", line 23, in <module>
    from . import v1
  File "C:\Users\Abdul\.conda\envs\tensorflow\lib\site-packages\tensorflow_core\_api\v2\compat\v1\__init__.py", line 40, in <module>
    from . import experimental
  File "C:\Users\Abdul\.conda\envs\tensorflow\lib\site-packages\tensorflow_core\_api\v2\compat\v1\experimental\__init__.py", line 11, in <module>
    from tensorflow.python.ops.control_flow_v2_toggles import output_all_intermediates
  File "C:\Users\Abdul\.conda\envs\tensorflow\lib\site-packages\tensorflow_core\python\ops\control_flow_v2_toggles.py", line 24, in <module>
    from tensorflow.python.ops import control_flow_util_v2
  File "C:\Users\Abdul\.conda\envs\tensorflow\lib\site-packages\tensorflow_core\python\ops\control_flow_util_v2.py", line 28, in <module>
    from tensorflow.python.keras.engine import base_layer_utils
  File "C:\Users\Abdul\.conda\envs\tensorflow\lib\site-packages\tensorflow_core\python\keras\__init__.py", line 27, in <module>
    from tensorflow.python.keras import applications
  File "C:\Users\Abdul\.conda\envs\tensorflow\lib\site-packages\tensorflow_core\python\keras\applications\__init__.py", line 25, in <module>
    from tensorflow.python.keras import engine
  File "C:\Users\Abdul\.conda\envs\tensorflow\lib\site-packages\tensorflow_core\python\keras\engine\__init__.py", line 23, in <module>
    from tensorflow.python.keras.engine.base_layer import Layer
  File "C:\Users\Abdul\.conda\envs\tensorflow\lib\site-packages\tensorflow_core\python\keras\engine\base_layer.py", line 56, in <module>
    from tensorflow.python.keras.saving.saved_model import layer_serialization
  File "C:\Users\Abdul\.conda\envs\tensorflow\lib\site-packages\tensorflow_core\python\keras\saving\__init__.py", line 20, in <module>
    from tensorflow.python.keras.saving.hdf5_format import load_attributes_from_hdf5_group
  File "C:\Users\Abdul\.conda\envs\tensorflow\lib\site-packages\tensorflow_core\python\keras\saving\hdf5_format.py", line 32, in <module>
    from tensorflow.python.keras.utils import conv_utils
  File "C:\Users\Abdul\.conda\envs\tensorflow\lib\site-packages\tensorflow_core\python\keras\utils\__init__.py", line 38, in <module>
    from tensorflow.python.keras.utils.multi_gpu_utils import multi_gpu_model
  File "C:\Users\Abdul\.conda\envs\tensorflow\lib\site-packages\tensorflow_core\python\keras\utils\multi_gpu_utils.py", line 22, in <module>
    from tensorflow.python.keras.engine.training import Model
  File "C:\Users\Abdul\.conda\envs\tensorflow\lib\site-packages\tensorflow_core\python\keras\engine\training.py", line 42, in <module>
    from tensorflow.python.keras import metrics as metrics_module
  File "C:\Users\Abdul\.conda\envs\tensorflow\lib\site-packages\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' (C:\Users\Abdul\.conda\envs\tensorflow\lib\site-packages\tensorflow_core\python\keras\engine\base_layer.py)
---------------------------------------------------------------------------

​
10

5 Answers

If you get this error on Google colab, then factory reset the runtime. Click on Runtime, then factory reset runtime

1

If happen in Google Colab:
In top menu click Runtime > Restart Runtime

I was getting the same error on a Windows 10 VM. Since you're using Anaconda, try:

pip uninstall tensorflow

Then try conda install tensorflow

Then try to import your Keras packages via: from tensorflow.keras.xxx import xxx

As mentioned in the comments above, it might be worth a try setting up a virtual environment in conda, and installing the required packages there.

In Anaconda you cannot see the pip uninstall prompts you with "Do you want to continue (Y/N)? Therefore you should add --yes: pip uninstall tensorflow --yes

I solved this issue by downloading and installing "Microsoft Visual C++ Redistributable for Visual Studio 2015, 2017 and 2019". It worked for me.

Pre-requisite: Python + Jupyter notebook + tensorflow installation already done

You get this error when you "import tensorflow"

Follow the below steps:

Step 1: Download "Microsoft Visual C++ Redistributable for Visual Studio 2015, 2017 and 2019 - community edition"

Step 2: Select "Python Workload" while installing Microsoft Visual C++ Redistributable for Visual Studio 2015, 2017 and 2019

Step 3: Restart your computer once

Step 4: That's it now open Jupyter notebook and "import tensorflow" and the error will not occur

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Robert Thorne
Author

Robert Thorne

Robert Thorne covers electric vehicle innovations, autonomous driving systems, global mobility trends, and automotive engineering developments.