Getting error, ModuleNotFoundError: No module named 'tensorflow.python.util
I've tried uninstalling and reinstalling more than 10 times, and tried different variations
pip install tensorflow_gpu, pip install tensorflow --user, and also upgraded my pip.
But still this error keeps happening when I try to import keras.models from tensorflow to run a model.
Does anyone know how to resolve the issue? Thanks in advance.
The full error I am getting:
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-67-114c1607a26b> in <module>
4 from datetime import datetime
5 import numpy as np
----> 6 from tensorflow import keras
7 from keras.models import Sequential
8 from keras.layers import LSTM
~\AppData\Roaming\Python\Python38\site-packages\tensorflow\__init__.py in <module>
39 import sys as _sys
40
---> 41 from tensorflow.python.tools import module_util as _module_util
42 from tensorflow.python.util.lazy_loader import LazyLoader as _LazyLoader
43
~\AppData\Roaming\Python\Python38\site-packages\tensorflow\python\__init__.py in <module>
38 # pylint: disable=wildcard-import,g-bad-import-order,g-import-not-at-top
39
---> 40 from tensorflow.python.eager import context
41 from tensorflow.python import pywrap_tensorflow as _pywrap_tensorflow
42
~\AppData\Roaming\Python\Python38\site-packages\tensorflow\python\eager\context.py in <module>
34 from tensorflow.core.protobuf import rewriter_config_pb2
35 from tensorflow.python import pywrap_tfe
---> 36 from tensorflow.python import tf2
37 from tensorflow.python.client import pywrap_tf_session
38 from tensorflow.python.eager import executor
~\AppData\Roaming\Python\Python38\site-packages\tensorflow\python\tf2.py in <module>
24
25 from tensorflow.python.platform import _pywrap_tf2
---> 26 from tensorflow.python.util.tf_export import tf_export
27
28
ModuleNotFoundError: No module named 'tensorflow.python.util
My guess is that pip is installing it for python 2 and then you are running in python 3. I have seen this behaviour and then needing to use pip3.
Related
I am having serious problems with sklearn. In particular, when doing:
from sklearn.linear_model import LinearRegression
I get the following error:
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-8-a35af2a4e137> in <module>
----> 1 from sklearn.linear_model import LinearRegression
~/anaconda3/lib/python3.7/site-packages/sklearn/__init__.py in <module>
78 from . import _distributor_init # noqa: F401
79 from . import __check_build # noqa: F401
---> 80 from .base import clone
81 from .utils._show_versions import show_versions
82
~/anaconda3/lib/python3.7/site-packages/sklearn/base.py in <module>
19 from . import __version__
20 from ._config import get_config
---> 21 from .utils import _IS_32BIT
22 from .utils.validation import check_X_y
23 from .utils.validation import check_array
~/anaconda3/lib/python3.7/site-packages/sklearn/utils/__init__.py in <module>
21
22 from .murmurhash import murmurhash3_32
---> 23 from .class_weight import compute_class_weight, compute_sample_weight
24 from . import _joblib
25 from ..exceptions import DataConversionWarning
~/anaconda3/lib/python3.7/site-packages/sklearn/utils/class_weight.py in <module>
5 import numpy as np
6
----> 7 from .validation import _deprecate_positional_args
8
9
~/anaconda3/lib/python3.7/site-packages/sklearn/utils/validation.py in <module>
23 from contextlib import suppress
24
---> 25 from .fixes import _object_dtype_isnan, parse_version
26 from .. import get_config as _get_config
27 from ..exceptions import NonBLASDotWarning, PositiveSpectrumWarning
ImportError: cannot import name 'parse_version' from 'sklearn.utils.fixes' (/Users/federiconutarelli/anaconda3/lib/python3.7/site-packages/sklearn/utils/fixes.py)
which I am not able to fix since in fixes.py I can see
def _parse_version(version_string):
version = []
for x in version_string.split('.'):
try:
version.append(int(x))
except ValueError:
# x may be of the form dev-1ea1592
version.append(x)
return tuple(version)
which seem to me the same as parse_version. Why Is this error occurring? Can anyone please help me?
Thank you all,
Federico
You may resolve this by creating a fresh conda environment. Sometimes some conflicts prevent packages from running correctly.
For my situation, I uninstall the two packages scipy and scikit-learn and then installed them. And then it works well. I try both conda install and pip install for many times, so I can't tell which way to install is better.
In the detailed error report, there should be some lines relevant to scipy. So I think this is an error about the incompatibility between these two packages and therefore some dll doesn't work well.
On Windows 10 with Python 3.8.3. I installed TensorFlow using
pip install tensorflow==2.0.0
but get the following error. Any recommendations on how I can fix this?
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
~\AppData\Roaming\Python\Python38\site-packages\tensorflow\python\pywrap_tensorflow.py in <module>
63 try:
---> 64 from tensorflow.python._pywrap_tensorflow_internal import *
65 # This try catch logic is because there is no bazel equivalent for py_extension.
ImportError: DLL load failed while importing _pywrap_tensorflow_internal: The specified module could not be found.
During handling of the above exception, another exception occurred:
ImportError Traceback (most recent call last)
<ipython-input-17-07ec96d933aa> in <module>
3 import matplotlib as mpl
4 import sklearn
----> 5 import tensorflow as tf
6 from tensorflow import keras
~\AppData\Roaming\Python\Python38\site-packages\tensorflow\__init__.py in <module>
39 import sys as _sys
40
---> 41 from tensorflow.python.tools import module_util as _module_util
42 from tensorflow.python.util.lazy_loader import LazyLoader as _LazyLoader
43
~\AppData\Roaming\Python\Python38\site-packages\tensorflow\python\__init__.py in <module>
38 # pylint: disable=wildcard-import,g-bad-import-order,g-import-not-at-top
39
---> 40 from tensorflow.python.eager import context
41
42 # pylint: enable=wildcard-import
~\AppData\Roaming\Python\Python38\site-packages\tensorflow\python\eager\context.py in <module>
33 from tensorflow.core.protobuf import config_pb2
34 from tensorflow.core.protobuf import rewriter_config_pb2
---> 35 from tensorflow.python import pywrap_tfe
36 from tensorflow.python import tf2
37 from tensorflow.python.client import pywrap_tf_session
~\AppData\Roaming\Python\Python38\site-packages\tensorflow\python\pywrap_tfe.py in <module>
26
27 # pylint: disable=invalid-import-order,g-bad-import-order, wildcard-import, unused-import
---> 28 from tensorflow.python import pywrap_tensorflow
29 from tensorflow.python._pywrap_tfe import *
~\AppData\Roaming\Python\Python38\site-packages\tensorflow\python\pywrap_tensorflow.py in <module>
81 for some common reasons and solutions. Include the entire stack trace
82 above this error message when asking for help.""" % traceback.format_exc()
---> 83 raise ImportError(msg)
84
85 # pylint: enable=wildcard-import,g-import-not-at-top,unused-import,line-too-long
ImportError: Traceback (most recent call last):
File "C:\Users\sunel\AppData\Roaming\Python\Python38\site-packages\tensorflow\python\pywrap_tensorflow.py", line 64, in <module>
from tensorflow.python._pywrap_tensorflow_internal import *
ImportError: DLL load failed while importing _pywrap_tensorflow_internal: The specified module could not be found.
Failed to load the native TensorFlow runtime.
Python 3.8 support starts TensorFlow 2.2 or later. Hence you are facing above issue.
You can install latest version of Tensorflow using pip install tensorflow. TensorFlow 2 packages require a pip version > 19.0.
You need to download and install/update the Microsoft Visual C++ 2015-2019 Redistributable (x64) from here.
If you are still facing DLL load failed errors, possible reasons are
Your CPU/Python is on 32 bits (It should be 64 bit)
There is a library that is in a different location/not installed on your system that cannot be loaded.
For more details please refer here.
Getting this error when trying to import tf_agents.environments running on Tensorflow 2.3.0, using anaconda environment. Have tried to reinstall tensorflow, still getting same error. Running the jupyter notebook in as admin, still getting same error. I am using python 3.8
from tf_agents.environments import suite_gym
ImportError Traceback (most recent call last)
<ipython-input-8-b9cd8ac2787f> in <module>
----> 1 from tf_agents.environments import suite_gym
2
3 # env = suite_gym.load("Breakout-v4")
4 # env
~\AppData\Roaming\Python\Python38\site-packages\tf_agents\environments\__init__.py in <module>
24 from tf_agents.environments import tf_py_environment
25 from tf_agents.environments import trajectory_replay
---> 26 from tf_agents.environments import utils
27 from tf_agents.environments import wrappers
~\AppData\Roaming\Python\Python38\site-packages\tf_agents\environments\utils.py in <module>
23 from tf_agents.environments import tf_environment
24 from tf_agents.environments import tf_py_environment
---> 25 from tf_agents.policies import random_py_policy
26 from tf_agents.specs import array_spec
27
~\AppData\Roaming\Python\Python38\site-packages\tf_agents\policies\__init__.py in <module>
16 """Policies Module."""
17
---> 18 from tf_agents.policies import actor_policy
19 from tf_agents.policies import boltzmann_policy
20 from tf_agents.policies import epsilon_greedy_policy
~\AppData\Roaming\Python\Python38\site-packages\tf_agents\policies\actor_policy.py in <module>
27 import tensorflow_probability as tfp
28
---> 29 from tf_agents.networks import network
30 from tf_agents.policies import tf_policy
31 from tf_agents.specs import tensor_spec
~\AppData\Roaming\Python\Python38\site-packages\tf_agents\networks\__init__.py in <module>
16 """Networks Module."""
17
---> 18 from tf_agents.networks import actor_distribution_network
19 from tf_agents.networks import actor_distribution_rnn_network
20 from tf_agents.networks import bias_layer
~\AppData\Roaming\Python\Python38\site-packages\tf_agents\networks\actor_distribution_network.py in <module>
24 import tensorflow as tf # pylint: disable=g-explicit-tensorflow-version-import
25
---> 26 from tf_agents.networks import categorical_projection_network
27 from tf_agents.networks import encoding_network
28 from tf_agents.networks import network
~\AppData\Roaming\Python\Python38\site-packages\tf_agents\networks\categorical_projection_network.py in <module>
24 import tensorflow_probability as tfp
25
---> 26 from tf_agents.networks import network
27 from tf_agents.networks import utils
28 from tf_agents.specs import distribution_spec
~\AppData\Roaming\Python\Python38\site-packages\tf_agents\networks\network.py in <module>
31
32 # pylint:disable=g-direct-tensorflow-import
---> 33 from tensorflow.python.keras.engine import network as keras_network # TF internal
34 from tensorflow.python.training.tracking import base # TF internal
35 from tensorflow.python.util import tf_decorator # TF internal
ImportError: cannot import name 'network' from 'tensorflow.python.keras.engine' (C:\Users\Vision\anaconda3\envs\tf2\lib\site-packages\tensorflow\python\keras\engine\__init__.py)
Am a bit late here, but faced a similar issue.
How I resolved the issue :
Uninstall the current version of tf_agents, use
pip uninstall tf-agents
Please install the current tf-agents-nightly (0.6.0) GitHub Repo.
use pip install tf-agents-nightly
from tf_agents.environments import suite_gym
Background
I'm trying to work on a GAN neural network (I'm a beginner for both Python and Machine-Learning), and I need Tensorflow.
Problem
I have tried to use TensorFlow but can't install. I have read questions and answers on SO about various errors, and have tested out those solutions, but I believe this case is different.
What I have tried (in chronological order)
1. Plain Reboot
a) Close all tabs for Jupyter Notebook
b) Close Anaconda Navigator
c) Restart Jupyter Notebook
d) Rerun code
Result: ImportError: no module
2. Reinstall tf
a) Repeat 1a and 1b
b) Open Anaconda Prompt
c) pip install tensorflow
Result: module installed
3. Check out Navigator
Tensorflow installed in all environments I have.
4. Reinstall tf (Take 2)
a) Repeat 1a and 1b
b) Open Anaconda Prompt
c) conda install -c conda-forge tensorflow
Result: EnvironmentNotWritableError:The current user does not have write permissions to the target environment. environment location: C:\ProgramData\<my username>
5. Run as admin (from this question)
a) Repeat 1a and 1b
b) Open Anaconda Prompt
c) conda install -c conda-forge tensorflow
Result:
Preparing transaction: done
Verifying transaction: done
Executing transaction: <after some text> done
6. Run in Jupyter
The code has nothing to do with TF at the moment, but still, it doesn't work.
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
import os
import glob2
font_lib = glob2.glob('**/*.ttf', recursive=True)
count = 0
for f in font_lib:
count = count + 1
if count < 10:
print (f)
else:
break
print ("done")
Result:
AttributeError Traceback (most recent call last)
<ipython-input-6-efbffb1990be> in <module>
----> 1 import tensorflow as tf
2 import numpy as np
3 import matplotlib.pyplot as plt
4 import os
5 import glob2
C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\__init__.py in <module>
22
23 # pylint: disable=g-bad-import-order
---> 24 from tensorflow.python import pywrap_tensorflow # pylint: disable=unused-import
25
26 from tensorflow._api.v1 import app
C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\__init__.py in <module>
80 from tensorflow.python import data
81 from tensorflow.python import distribute
---> 82 from tensorflow.python import keras
83 from tensorflow.python.feature_column import feature_column_lib as feature_column
84 from tensorflow.python.layers import layers
C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\keras\__init__.py in <module>
23
24 from tensorflow.python.keras import activations
---> 25 from tensorflow.python.keras import applications
26 from tensorflow.python.keras import backend
27 from tensorflow.python.keras import callbacks
C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\keras\applications\__init__.py in <module>
24 from tensorflow.python.keras import backend
25 from tensorflow.python.keras import engine
---> 26 from tensorflow.python.keras import layers
27 from tensorflow.python.keras import models
28 from tensorflow.python.keras import utils
C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\keras\layers\__init__.py in <module>
27
28 # Advanced activations.
---> 29 from tensorflow.python.keras.layers.advanced_activations import LeakyReLU
30 from tensorflow.python.keras.layers.advanced_activations import PReLU
31 from tensorflow.python.keras.layers.advanced_activations import ELU
C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\keras\layers\advanced_activations.py in <module>
25 from tensorflow.python.keras.engine.base_layer import Layer
26 from tensorflow.python.keras.engine.input_spec import InputSpec
---> 27 from tensorflow.python.keras.utils import tf_utils
28 from tensorflow.python.ops import math_ops
29 from tensorflow.python.util.tf_export import tf_export
C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\keras\utils\__init__.py in <module>
36 from tensorflow.python.keras.utils.layer_utils import get_source_inputs
37 from tensorflow.python.keras.utils.losses_utils import squeeze_or_expand_dimensions
---> 38 from tensorflow.python.keras.utils.multi_gpu_utils import multi_gpu_model
39 from tensorflow.python.keras.utils.np_utils import normalize
40 from tensorflow.python.keras.utils.np_utils import to_categorical
C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\keras\utils\multi_gpu_utils.py in <module>
20 from tensorflow.python.framework import ops
21 from tensorflow.python.keras import backend as K
---> 22 from tensorflow.python.keras.engine.training import Model
23 from tensorflow.python.ops import array_ops
24 from tensorflow.python.util.tf_export import tf_export
C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\keras\engine\training.py in <module>
40 from tensorflow.python.keras.engine import training_generator
41 from tensorflow.python.keras.engine import training_utils
---> 42 from tensorflow.python.keras.engine.network import Network
43 from tensorflow.python.keras.optimizer_v2 import optimizer_v2
44 from tensorflow.python.keras.utils import data_utils
C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\keras\engine\network.py in <module>
38 from tensorflow.python.keras.engine import base_layer
39 from tensorflow.python.keras.engine import base_layer_utils
---> 40 from tensorflow.python.keras.engine import saving
41 from tensorflow.python.keras.engine import training_utils
42 from tensorflow.python.keras.utils import generic_utils
C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\keras\engine\saving.py in <module>
36 # pylint: disable=g-import-not-at-top
37 try:
---> 38 import h5py
39 HDF5_OBJECT_HEADER_LIMIT = 64512
40 except ImportError:
C:\ProgramData\Anaconda3\lib\site-packages\h5py\__init__.py in <module>
34 _errors.silence_errors()
35
---> 36 from ._conv import register_converters as _register_converters
37 _register_converters()
38
h5py\h5r.pxd in init h5py._conv()
h5py\h5r.pyx in init h5py.h5r()
AttributeError: type object 'h5py.h5r.Reference' has no attribute '__reduce_cython__'
7. Update h5py
a) Followed #Alireza Tajadod's instructions and tried.
b) Run code in Jupyter
Result: Same as 6)...
I have tried every method I could, and any help would be highly appreciated. Thank you in advance!
Edit:
Reminded by the answer by #GarytheIceBreaker: Sorry that I forgot to mention, but I have everything installed and set up in Windows. Although this might be frustrating to some, please suggest solutions that can be done within Windows OS premises. Thanks!
I tried jumping through hoops on Windows, and did get Anaconda working, but not Tensorflow. I recommend running Ubuntu virtually, on WSL if you don't want to make any major changes to your machine. Ubuntu is pretty user friendly these days, even if you use it without any graphical shell enabled.
Enable WSL, install Ubuntu from the Microsoft store, and apt-get install tensorflow.
I was using this code:
import tensorflow as tf , and it was working fine.
When I now try to import it, I get the following error
In [2]: import tensorflow as tf
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-2-41389fad42b5> in <module>()
----> 1 import tensorflow as tf
/usr/local/lib/python2.7/dist-packages/tensorflow/__init__.py in <module>()
21 from __future__ import print_function
22
---> 23 from tensorflow.python import *
/usr/local/lib/python2.7/dist-packages/tensorflow/python/__init__.py in <module>()
63 from tensorflow.core.util.event_pb2 import *
64 # Import things out of contrib
---> 65 import tensorflow.contrib as contrib
66
67 # Framework
/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/__init__.py in <module>()
28 from tensorflow.contrib import grid_rnn
29 from tensorflow.contrib import layers
---> 30 from tensorflow.contrib import learn
31 from tensorflow.contrib import linear_optimizer
32 from tensorflow.contrib import lookup
/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/learn/__init__.py in <module>()
70
71 # pylint: disable=wildcard-import
---> 72 from tensorflow.contrib.learn.python.learn import *
73 from tensorflow.python.util.all_util import make_all
74
/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/learn/python/__init__.py in <module>()
21
22 # pylint: disable=wildcard-import
---> 23 from tensorflow.contrib.learn.python.learn import *
/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/learn/python/learn/__init__.py in <module>()
24 # pylint: disable=wildcard-import
25 from tensorflow.contrib.learn.python.learn import datasets
---> 26 from tensorflow.contrib.learn.python.learn import estimators
27 from tensorflow.contrib.learn.python.learn import graph_actions
28 from tensorflow.contrib.learn.python.learn import learn_io as io
/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/learn/python/learn/estimators/__init__.py in <module>()
21
22 from tensorflow.contrib.learn.python.learn.estimators._sklearn import NotFittedError
---> 23 from tensorflow.contrib.learn.python.learn.estimators.autoencoder import TensorFlowDNNAutoencoder
24 from tensorflow.contrib.learn.python.learn.estimators.base import TensorFlowBaseTransformer
25 from tensorflow.contrib.learn.python.learn.estimators.base import TensorFlowEstimator
/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/learn/python/learn/estimators/autoencoder.py in <module>()
23
24 from tensorflow.contrib.learn.python.learn import models
---> 25 from tensorflow.contrib.learn.python.learn.estimators.base import TensorFlowBaseTransformer
26 from tensorflow.python.ops import nn
27
/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/learn/python/learn/estimators/base.py in <module>()
32 from tensorflow.contrib import layers
33 from tensorflow.contrib.learn.python.learn.estimators import _sklearn
---> 34 from tensorflow.contrib.learn.python.learn.estimators import estimator
35 from tensorflow.contrib.learn.python.learn.estimators._sklearn import NotFittedError
36 from tensorflow.contrib.learn.python.learn.learn_io.data_feeder import setup_train_data_feeder
/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/learn/python/learn/estimators/estimator.py in <module>()
37 from tensorflow.contrib.learn.python.learn.estimators import tensor_signature
38 from tensorflow.contrib.learn.python.learn.estimators._sklearn import NotFittedError
---> 39 from tensorflow.contrib.learn.python.learn.learn_io import data_feeder
40 from tensorflow.contrib.learn.python.learn.utils import checkpoints
41
/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/learn/python/learn/learn_io/__init__.py in <module>()
28 from tensorflow.contrib.learn.python.learn.learn_io.graph_io import read_keyed_batch_examples
29 from tensorflow.contrib.learn.python.learn.learn_io.graph_io import read_keyed_batch_features
---> 30 from tensorflow.contrib.learn.python.learn.learn_io.pandas_io import extract_pandas_data
31 from tensorflow.contrib.learn.python.learn.learn_io.pandas_io import extract_pandas_labels
32 from tensorflow.contrib.learn.python.learn.learn_io.pandas_io import extract_pandas_matrix
/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/learn/python/learn/learn_io/pandas_io.py in <module>()
22 try:
23 # pylint: disable=g-import-not-at-top
---> 24 import pandas as pd
25 HAS_PANDAS = True
26 except ImportError:
/usr/local/lib/python2.7/dist-packages/pandas/__init__.py in <module>()
23
24 try:
---> 25 from pandas import hashtable, tslib, lib
26 except ImportError as e: # pragma: no cover
27 module = str(e).lstrip('cannot import name ') # hack but overkill to use re
/home/kv/pandas/src/numpy.pxd in init pandas.hashtable (pandas/hashtable.c:38364)()
ValueError: numpy.dtype has the wrong size, try recompiling. Expected 88, got 96
I even tried upgrading numpy, but that didn't solve the issue.
Can you help me resolve this error?
EDIT:
numpy (1.8.2)
tensorflow (0.10.0rc0)
python 2.7.6
You need to update numpy or recompile pandas.
There is a very good answer and explanation provided here: ValueError: numpy.dtype has the wrong size, try recompiling
I would strongly suggest to use the anaconda distribution and install the latest tensorflow using:
conda install -c conda-forge tensorflow
Read more in the official setup guide
Try installing numpy with PIP using
sudo apt-get install python-pip
sudo pip install numpy==1.11.1
or pip3 instead of pip for python 3 like
sudo apt-get install python3-pip
sudo pip3 install numpy==1.11.1
this will help as i also had this error because tensorflow is using numpy 1.11.1