Python 安装后无法导入keras

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39930952/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 22:56:01  来源:igfitidea点击:

Cannot import keras after installation

pythonubuntutensorflowanacondakeras

提问by nabroyan

I'm trying to setup kerasdeep learning library for Python3.5on Ubuntu 16.04 LTS and use Tensorflowas a backend. I have Python2.7and Python3.5installed. I have installed Anacondaand with help of it Tensorflow, numpy, scipy, pyyaml. Afterwards I have installed keraswith command

我正在尝试在 Ubuntu 16.04 LTS 上设置keras深度学习库Python3.5Tensorflow用作后端。我有Python2.7Python3.5安装。我已经安装Anaconda并在它的帮助下 Tensorflownumpy, scipy, pyyaml. 之后我keras用命令安装

sudo python setup.py install

须藤 python setup.py 安装

Although I can see /usr/local/lib/python3.5/dist-packages/Keras-1.1.0-py3.5.eggdirectory, I cannot use keraslibrary. When I try to import it in python it says

虽然我可以看到/usr/local/lib/python3.5/dist-packages/Keras-1.1.0-py3.5.egg目录,但我不能使用keras库。当我尝试在 python 中导入它时,它说

ImportError: No module named 'keras'

导入错误:没有名为“keras”的模块

I have tried to install kerasusingpip3, but got the same result.

我曾试图安装keras使用pip3,但得到了同样的结果。

What am I doing wrong? Any Ideas?

我究竟做错了什么?有任何想法吗?

采纳答案by Laurent LAPORTE

Diagnose

诊断

If you have pipinstalled (you should have it until you use Python 3.5), list the installed Python packages, like this:

如果您已经pip安装(在使用 Python 3.5 之前应该安装它),列出已安装的 Python 包,如下所示:

$ pip list | grep -i keras
Keras (1.1.0)

If you don't see Keras, it means that the previous installation failed or is incomplete (this lib has this dependancies: numpy (1.11.2), PyYAML (3.12), scipy (0.18.1), six (1.10.0), and Theano (0.8.2).)

如果没有看到Keras,说明之前的安装失败或者不完整(这个lib有这个依赖:numpy (1.11.2), PyYAML (3.12), scipy (0.18.1), Six (1.10.0) , 和 Theano (0.8.2).)

Consult the pip.logto see what's wrong.

咨询pip.log看看有什么问题。

You can also display your Python path like this:

您还可以像这样显示您的 Python 路径:

$ python3 -c 'import sys, pprint; pprint.pprint(sys.path)'
['',
 '/Library/Frameworks/Python.framework/Versions/3.5/lib/python35.zip',
 '/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5',
 '/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/plat-darwin',
 '/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/lib-dynload',
 '/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages']

Make sure the Keras library appears in the /Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packagespath (the path is different on Ubuntu).

确保 Keras 库出现在/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages路径中(Ubuntu 上的路径不同)。

If not, try do uninstall it, and retry installation:

如果没有,请尝试卸载它,然后重试安装:

$ pip uninstall Keras

Use a virtualenv

使用虚拟环境

It's a bad idea to use and pollute your system-wide Python. I recommend using a virtualenv (see this guide).

使用和污染系统范围的 Python 是一个坏主意。我建议使用 virtualenv(请参阅本指南)。

The best usage is to create a virtualenvdirectory (in your home, for instance), and store your virtualenvs in:

最好的用法是创建一个virtualenv目录(例如在您的家中),并将您的 virtualenv 存储在:

cd virtualenv/
virtualenv -p python3.5 py-keras
source py-keras/bin/activate
pip install -q -U pip setuptools wheel

Then install Keras:

然后安装 Keras:

pip install keras

You get:

你得到:

$ pip list
Keras (1.1.0)
numpy (1.11.2)
pip (8.1.2)
PyYAML (3.12)
scipy (0.18.1)
setuptools (28.3.0)
six (1.10.0)
Theano (0.8.2)
wheel (0.30.0a0)

But, you also need to install extra libraries, like Tensorflow:

但是,您还需要安装额外的库,例如 Tensorflow:

$ python -c "import keras"
Using TensorFlow backend.
Traceback (most recent call last):
  ...
ImportError: No module named 'tensorflow'

The installation guide of TesnsorFlow is here: https://www.tensorflow.org/versions/r0.11/get_started/os_setup.html#pip-installation

TesnsorFlow的安装指南在这里:https://www.tensorflow.org/versions/r0.11/get_started/os_setup.html#pip-installation

回答by Beltino Goncalves

Ran to the same issue, Assuming your using anaconda3 and your using a venvwith >= python=3.6:

遇到同样的问题,假设您使用 anaconda3 并使用venvwith >= python=3.6

python -m pip install keras
sudo python -m pip install --user tensorflow

回答by angelo.mastro

I had pipreferring by default to pip3, which made me download the libs for python3. On the contrary I launched the shell as python(which opened python 2) and the library wasn't installed there obviously.

pip默认指的是 pip3,这让我下载了 python3 的库。相反,我将 shell 启动为python(打开了 python 2)并且显然没有安装库。

Once I matched the names pip3 -> python3, pip -> python (2) all worked.

一旦我匹配了名称 pip3 -> python3, pip -> python (2) 一切正常。

回答by AAKANKSHA JAIN

Firstly checked the list of installed Python packages by:

首先通过以下方式检查已安装的 Python 包列表:

pip list | grep -i keras

点子列表| grep -i keras

If there is keras shown then install it by:

如果显示 keras,则通过以下方式安装它:

pip install keras --upgrade --log ./pip-keras.log

pip install keras --upgrade --log ./pip-keras.log

now check the log, if there is any pending dependencies are present, it will affect your installation. So remove dependencies and then again install it.

现在检查日志,如果存在任何挂起的依赖项,它将影响您的安装。所以删除依赖项,然后再次安装它。