Python 导入错误:无法导入名称 np_utils
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45149341/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ImportError: cannot import name np_utils
提问by Elizabeth Fons
I'm trying to run the following examplefrom keras
我正在尝试从 keras运行以下示例
but I get this error:
但我收到此错误:
ImportError
Traceback (most recent call last)
<ipython-input-58-50de27eea0f8> in <module>()
8 import numpy as np
9 import matplotlib.pyplot as plt
---> 10 from keras.models import Sequential
11 from keras.layers import Dense, LSTM
12
/usr/local/lib/python2.7/dist-packages/keras/__init__.py in <module>()
1 from __future__ import absolute_import
2
----> 3 from . import utils
4 from . import activations
5 from . import applications
/usr/local/lib/python2.7/dist-packages/keras/utils/__init__.py in <module>()
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'm using Ubuntu and I installed keras with:
我正在使用 Ubuntu,我安装了 keras:
sudo pip install keras
This question was already asked but there was no answer: Keras: Cannot Import Name np_utils
这个问题已经被问过了,但没有答案: Keras:无法导入名称 np_utils
回答by Kunal Goswami
Try importing numpy
before you import something from keras
(I see that you have already done so, am adding this just to document the solution which worked for me). I faced the same error and when I tried:
numpy
在从中导入某些内容之前尝试导入keras
(我看到您已经这样做了,我添加这个只是为了记录对我有用的解决方案)。我遇到了同样的错误,当我尝试时:
import numpy as np
from __future__ import absolute_import
#Anything from keras
It seemed to work just fine with me. Try installing the latest stable packages of future
and numpy
beforehand through:
它对我来说似乎很好用。尝试安装最新的稳定的软件包future
,并numpy
事先通过:
pip install future
pip install numpy
Sometimes its possible that condaand other installations of python might be interfering with each other. I had everything managed through brew beforehand, but when I installed condamany of the packages which I previously installed gave me an import error (because of the PYTHONPATH
variable).
有时,conda和其他 python 安装可能会相互干扰。我事先通过 brew 管理了所有内容,但是当我安装conda时,我之前安装的许多软件包都给了我一个导入错误(因为PYTHONPATH
变量)。
回答by Nabeel Ahmed
np_utils
is a separate package (and a keras dependency - which doesn't get install with it). Can be installed using pip:
np_utils
是一个单独的包(和一个 keras 依赖项 - 没有安装它)。可以使用pip安装:
pip install np_utils
using - Keras==2.0.6
使用 - Keras==2.0.6
Suggestion: For some odd (and still unknown) reasons, even after installing the import
建议:出于一些奇怪(仍然未知)的原因,即使在安装导入之后
from keras.utils.np_utils import to_categorical
didn't work - I had to restart the notebook (first restart even didn't work), and once it worked, I got stuck again for same import call (gave exception for no module named tensorflow
) - as in utils there's another import from . import conv_utils
, which required the tensorflow.
没有用 - 我不得不重新启动笔记本(第一次重启甚至没有用),一旦它成功了,我又被同样的导入调用卡住了(给出了例外no module named tensorflow
) - 因为在 utils 中有另一个 import from . import conv_utils
,它需要张量流。
I did try installing tensorflow using pip install tensorflow gave:
我确实尝试使用 pip install tensorflow 安装 tensorflow 给出:
Could not find a version that satisfies the requirement tensorflow (from versions: ) No matching distribution found for tensorflow
找不到满足张量流要求的版本(来自版本:)没有找到与张量流匹配的分布
even this gistdidn't work for me.
即使这个要点对我也不起作用。
Finally, I installedAnaconda - which have all the scientific packages (numpy, scipy, scikit-learn,..) pre-installed. Installed keras:
最后,我安装了Anaconda - 它预先安装了所有科学软件包(numpy、scipy、scikit-learn 等)。安装的keras:
conda install keras
Best thing was, it even installed tensorflow as its dependency.
最好的事情是,它甚至安装了 tensorflow 作为它的依赖项。
回答by muninn
I ran into the same issue. You need to do pip install np_utils and then restart your terminal. Make sure everything is up to date.
我遇到了同样的问题。您需要执行 pip install np_utils ,然后重新启动终端。确保一切都是最新的。
回答by Daniel McLean
I had to install tensorflow to solve this problem. (From virtualenv):
我不得不安装 tensorflow 来解决这个问题。(来自 virtualenv):
pip install tensorflow
回答by Mimii Cheng
For keras > 2.0, please use from keras.utils import to_categorical
instead.
对于keras > 2.0,请from keras.utils import to_categorical
改用。
Example of usage will be to_categorical(y, num_classes=None)
使用示例将是 to_categorical(y, num_classes=None)
回答by mohaseeb
I had a similar issue in a build system:
我在构建系统中遇到了类似的问题:
- Keras throwing: ImportError: cannot import name np_utils
- But also tensorflow assertion failure: AttributeError: type object 'NewBase' has no attribute 'is_abstract'
- Keras 抛出:ImportError:无法导入名称 np_utils
- 但也 tensorflow 断言失败: AttributeError: type object 'NewBase' has no attribute 'is_abstract'
The problem in my case was the build environment, for some reason I didn't investigate, had an old six version (six 1.5.0) (compared to my local env). The issue was solved by installing the most recent six version (1.11.0 when writing this).
我的问题是构建环境,出于某种原因我没有调查,有一个旧的六个版本(六个 1.5.0)(与我的本地环境相比)。通过安装最新的六个版本(编写本文时为 1.11.0)解决了该问题。
pip install six -U
pip install six -U
回答by stenlytw
Try to install the old version using Anaconda:
尝试使用 Anaconda 安装旧版本:
conda install tensorflow-gpu==1.2.1
回答by shinemu
Open Anaconda Prompt --> Write this command : **conda install keras**
(base) C:\>conda `enter code here`install keras
Collecting package metadata: done
Solving environment: done
## Package Plan ##
environment location: C:\Users\sinem.secgin\AppData\Local\Continuum\anaconda3
added / updated specs:
- keras
The following packages will be downloaded:
package | build
---------------------------|-----------------
_tflow_select-2.3.0 | mkl 3 KB
absl-py-0.7.1 | py37_0 158 KB
astor-0.7.1 | py37_0 44 KB
ca-certificates-2019.5.15 | 0 166 KB
certifi-2019.6.16 | py37_0 155 KB
conda-4.7.5 | py37_0 3.0 MB
conda-package-handling-1.3.10| py37_0 280 KB
gast-0.2.2 | py37_0 138 KB
grpcio-1.16.1 | py37h351948d_1 947 KB
keras-2.2.4 | 0 5 KB
keras-applications-1.0.8 | py_0 33 KB
keras-base-2.2.4 | py37_0 489 KB
keras-preprocessing-1.1.0 | py_1 36 KB
libmklml-2019.0.3 | 0 21.4 MB
libprotobuf-3.8.0 | h7bd577a_0 2.2 MB
markdown-3.1.1 | py37_0 132 KB
mock-3.0.5 | py37_0 47 KB
openssl-1.1.1c | he774522_1 5.7 MB
protobuf-3.8.0 | py37h33f27b4_0 581 KB
tensorboard-1.13.1 | py37h33f27b4_0 3.3 MB
tensorflow-1.13.1 |mkl_py37h9463c59_0 4 KB
tensorflow-base-1.13.1 |mkl_py37hcaf7020_0 49.4 MB
tensorflow-estimator-1.13.0| py_0 205 KB
termcolor-1.1.0 | py37_1 7 KB
------------------------------------------------------------
Total: 88.4 MB
The following NEW packages will be INSTALLED:
_tflow_select pkgs/main/win-64::_tflow_select-2.3.0-mkl
absl-py pkgs/main/win-64::absl-py-0.7.1-py37_0
astor pkgs/main/win-64::astor-0.7.1-py37_0
conda-package-han~ pkgs/main/win-64::conda-package-handling-1.3.10-py37_0
gast pkgs/main/win-64::gast-0.2.2-py37_0
grpcio pkgs/main/win-64::grpcio-1.16.1-py37h351948d_1
keras pkgs/main/win-64::keras-2.2.4-0
keras-applications pkgs/main/noarch::keras-applications-1.0.8-py_0
keras-base pkgs/main/win-64::keras-base-2.2.4-py37_0
keras-preprocessi~ pkgs/main/noarch::keras-preprocessing-1.1.0-py_1
libmklml pkgs/main/win-64::libmklml-2019.0.3-0
libprotobuf pkgs/main/win-64::libprotobuf-3.8.0-h7bd577a_0
markdown pkgs/main/win-64::markdown-3.1.1-py37_0
mock pkgs/main/win-64::mock-3.0.5-py37_0
protobuf pkgs/main/win-64::protobuf-3.8.0-py37h33f27b4_0
tensorboard pkgs/main/win-64::tensorboard-1.13.1-py37h33f27b4_0
tensorflow pkgs/main/win-64::tensorflow-1.13.1-mkl_py37h9463c59_0
tensorflow-base pkgs/main/win-64::tensorflow-base-1.13.1-mkl_py37hcaf7020_0
tensorflow-estima~ pkgs/main/noarch::tensorflow-estimator-1.13.0-py_0
termcolor pkgs/main/win-64::termcolor-1.1.0-py37_1
Proceed ([y]/n)? y
Y
回答by Italo Gervasio
If you are using TensorFlow
backend with Keras
make sure your keras.json file states its backend is Tensorflow. The code below worked for me:
如果您使用TensorFlow
后端,请Keras
确保您的 keras.json 文件声明其后端是 Tensorflow。下面的代码对我有用:
import os
os.environ['KERAS_BACKEND']='tensorflow'
#Anything from keras
Cheers hope I helped somebody. OBS: I was using Anaconda and Spyder.
干杯希望我帮助了某人。OBS:我使用的是 Anaconda 和 Spyder。
回答by Matthieu
This problem seems to have different solutions depending on the situation. Here's yet another solution that helped me when I?had those exact symptoms:
这个问题似乎根据情况有不同的解决方案。当我有这些确切症状时,这是另一种帮助我的解决方案:
pip install enum34
Installing np_utils, future or a different version of numpy or Theano didn't help for me. The problem was due to Keras that use enum, that only exists in Python3. Enum34 is a backport of Python3's enum to Python2.
安装 np_utils、future 或不同版本的 numpy 或 Theano 对我没有帮助。问题是由于 Keras 使用枚举,仅存在于 Python3 中。Enum34 是 Python3 的枚举到 Python2 的反向移植。
I was using:
我正在使用:
- python2.7
- Keras==2.3.0
- Theano==1.0.4 as a backend
- 蟒蛇2.7
- Keras==2.3.0
- Theano==1.0.4 作为后端