Keras: ImportError: cannot import name np_utils

Created on 19 Dec 2017  路  20Comments  路  Source: keras-team/keras

Hi all,

I installed Keras with Anaconda on 2.7 python environement.

When I do the following : import keras : I get the following error and it stack. I tried everything I could find on stack and google without any help. I even tried installing it on a freshly installed environement without any improvements.

----> 1 import keras

/home/mohamed/.local/lib/python2.7/site-packages/keras/__init__.py in ()
1 from __future__ import absolute_import
2
----> 3 from . import utils
4 from . import activations
5 from . import applications

/home/mohamed/.local/lib/python2.7/site-packages/keras/utils/__init__.py in ()
1 from __future__ import absolute_import
----> 2 from . import np_utils
3 from . import generic_utils
4 from . import data_utils
5 from . import io_utils

ImportError: cannot import name np_utils

I checked the files. I am pretty sure they are there.

Most helpful comment

try this from tensorflow.python.keras.utils import np_utils

All 20 comments

From StackOverflow Link
Installing np_utils using pip worked.

Well. As I said i tried pretty much everything.Even this didn't work.

@dma092 What error are you encountering after running pip install? This seems like a pip issue rather than an issue with keras

I am seeing the same error after running pip install. It is actually a relative import.

same here , nothing worked

Same here nothing worked.

The same problem.

Same here too :(

Hi everyone, if you guys are planning to use tensorflow with keras make sure your keras.json file states its backend is tensorflow. For me I installed mxnet and the backend changed to mxnet so I too met this problem. After changing it to tensorflow it worked for me. Cheers hope I helped somebody

Or just do this

import os
os.environ['KERAS_BACKEND']='tensorflow'

Still seeing this issue with 2.1.5, theano backend, and np_utils installed

In the public API, you cannot access np_util (it's internal). You are supposed to access utilities via the utils module, e.g.

from keras import utils

utils.to_categorical(...)

I have the same issue
Even if i tried previous solution it is giving the same error

ImportError Traceback (most recent call last)
in ()
5 #from keras.layers import Dense, Conv2D, MaxPool2D, Flatten, Dropout
6 #from keras.optimizers import Adam
----> 7 from keras import utils
8 #from keras.utils.np_utils import to_categorical

/usr/local/lib/python2.7/site-packages/keras/__init__.py in ()
1 from __future__ import absolute_import
2
----> 3 from . import utils
4 from . import activations
5 from . import applications

/usr/local/lib/python2.7/site-packages/keras/utils/__init__.py in ()
1 from __future__ import absolute_import
----> 2 from . import np_utils
3 from . import generic_utils
4 from . import data_utils
5 from . import io_utils

ImportError: cannot import name np_utils

any news about this ? still doesn't work october with the same issue eventhough i import np_utils without any issue the previous line..

I first encounter the attached stack trace.
After I try running the jupyter notebook cell again, it turned into cannot import np_utils.
What's the connection between these two?

My keras version is 2.8.
I tried pip install --upgrade keras
I tried setting os environment to be tensorflow
I installed np_utils.

Any help would be appreciated!


ImportError Traceback (most recent call last)
in ()
9 from os.path import isfile, join
10
---> 11 from keras.models import Sequential, Model, load_model
12 from keras.layers import Input, Dense, Dropout, Activation, Flatten, Reshape, noise
13 from keras.layers import Convolution2D, MaxPooling2D, Convolution3D, MaxPooling3D, LSTM

/homes/hgaoab/.local/lib/python2.7/site-packages/keras/__init__.py in ()
1 from __future__ import absolute_import
2
----> 3 from . import utils
4 from . import activations
5 from . import applications

/homes/hgaoab/.local/lib/python2.7/site-packages/keras/utils/__init__.py in ()
4 from . import data_utils
5 from . import io_utils
----> 6 from . import conv_utils
7
8 # Globally-importable utils.

/homes/hgaoab/.local/lib/python2.7/site-packages/keras/utils/conv_utils.py in ()
7 from six.moves import range
8 import numpy as np
----> 9 from .. import backend as K
10
11

/homes/hgaoab/.local/lib/python2.7/site-packages/keras/backend/__init__.py in ()
87 elif _BACKEND == 'tensorflow':
88 sys.stderr.write('Using TensorFlow backend.\n')
---> 89 from .tensorflow_backend import *
90 else:
91 # Try and load external backend.

/homes/hgaoab/.local/lib/python2.7/site-packages/keras/backend/tensorflow_backend.py in ()
3 from __future__ import print_function
4
----> 5 import tensorflow as tf
6 from tensorflow.python.framework import ops as tf_ops
7 from tensorflow.python.training import moving_averages

/usr/local/packages/python/modules/tensorflow-1.8/lib/python3.6/site-packages/tensorflow/__init__.py in ()
22
23 # pylint: disable=g-bad-import-order
---> 24 from tensorflow.python import pywrap_tensorflow # pylint: disable=unused-import
25 # pylint: disable=wildcard-import
26 from tensorflow.tools.api.generator.api import * # pylint: disable=redefined-builtin

/usr/local/packages/python/modules/tensorflow-1.8/lib/python3.6/site-packages/tensorflow/python/__init__.py in ()
47 import numpy as np
48
---> 49 from tensorflow.python import pywrap_tensorflow
50
51 # Protocol buffers

/usr/local/packages/python/modules/tensorflow-1.8/lib/python3.6/site-packages/tensorflow/python/pywrap_tensorflow.py in ()
72 for some common reasons and solutions. Include the entire stack trace
73 above this error message when asking for help.""" % traceback.format_exc()
---> 74 raise ImportError(msg)
75
76 # pylint: enable=wildcard-import,g-import-not-at-top,unused-import,line-too-long

ImportError: Traceback (most recent call last):
File "/usr/local/packages/python/modules/tensorflow-1.8/lib/python3.6/site-packages/tensorflow/python/pywrap_tensorflow.py", line 58, in
from tensorflow.python.pywrap_tensorflow_internal import *
File "/usr/local/packages/python/modules/tensorflow-1.8/lib/python3.6/site-packages/tensorflow/python/pywrap_tensorflow_internal.py", line 28, in
_pywrap_tensorflow_internal = swig_import_helper()
File "/usr/local/packages/python/modules/tensorflow-1.8/lib/python3.6/site-packages/tensorflow/python/pywrap_tensorflow_internal.py", line 24, in swig_import_helper
_mod = imp.load_module('_pywrap_tensorflow_internal', fp, pathname, description)
ImportError: /usr/local/packages/python/modules/tensorflow-1.8/lib/python3.6/site-packages/tensorflow/python/_pywrap_tensorflow_internal.so: undefined symbol: PyUnicode_FromString

Failed to load the native TensorFlow runtime.

See https://www.tensorflow.org/install/install_sources#common_installation_problems

for some common reasons and solutions. Include the entire stack trace
above this error message when asking for help.

I tried to update tenforflow and solved this problem

It has been updated to keras.utils

from keras.utils import to_categorical

Or just do this

import os
os.environ['KERAS_BACKEND']='tensorflow'

Thanks @Dicksonchin93 ! It worked for me!

try this from tensorflow.python.keras.utils import np_utils

Python 3.7
TF version 2.1.0
Keras version 2.2.4-tf
this worked for me : from tensorflow.keras import utils

Let me ask my AI..

Was this page helpful?
0 / 5 - 0 ratings