【问题标题】:Keras can't be imported in Virtual Env虚拟环境中无法导入 Keras
【发布时间】:2017-01-19 01:58:26
【问题描述】:

知道如何解决这个问题吗?

mona@pascal:~$ python3
Python 3.4.3 (default, Nov 17 2016, 01:08:31) 
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import keras
Using TensorFlow backend.
I tensorflow/stream_executor/dso_loader.cc:128] successfully opened CUDA library libcublas.so locally
I tensorflow/stream_executor/dso_loader.cc:128] successfully opened CUDA library libcudnn.so locally
I tensorflow/stream_executor/dso_loader.cc:128] successfully opened CUDA library libcufft.so locally
I tensorflow/stream_executor/dso_loader.cc:128] successfully opened CUDA library libcuda.so.1 locally
I tensorflow/stream_executor/dso_loader.cc:128] successfully opened CUDA library libcurand.so locally
mona@pascal:~$ source /usr/local/bin/virtualenvwrapper.sh
mona@pascal:~$ workon cv2
(cv2) mona@pascal:~$ python
Python 3.4.3 (default, Nov 17 2016, 01:08:31) 
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import keras
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named 'keras'

更新:

$ sudo pip install keras
The directory '/home/mona/.cache/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
The directory '/home/mona/.cache/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Collecting keras
/usr/local/lib/python2.7/dist-packages/pip/_vendor/requests/packages/urllib3/util/ssl_.py:318: SNIMissingWarning: An HTTPS request has been made, but the SNI (Subject Name Indication) extension to TLS is not available on this platform. This may cause the server to present an incorrect TLS certificate, which can cause validation failures. You can upgrade to a newer version of Python to solve this. For more information, see https://urllib3.readthedocs.io/en/latest/security.html#snimissingwarning.
  SNIMissingWarning
/usr/local/lib/python2.7/dist-packages/pip/_vendor/requests/packages/urllib3/util/ssl_.py:122: InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. You can upgrade to a newer version of Python to solve this. For more information, see https://urllib3.readthedocs.io/en/latest/security.html#insecureplatformwarning.
  InsecurePlatformWarning
  Downloading Keras-1.2.0.tar.gz (167kB)
    100% |████████████████████████████████| 174kB 2.1MB/s 
Collecting theano (from keras)
  Downloading Theano-0.8.2.tar.gz (2.9MB)
    100% |████████████████████████████████| 2.9MB 346kB/s 
Requirement already satisfied: pyyaml in /usr/lib/python2.7/dist-packages (from keras)
Requirement already satisfied: six in /usr/local/lib/python2.7/dist-packages (from keras)
Requirement already satisfied: numpy>=1.7.1 in /usr/local/lib/python2.7/dist-packages (from theano->keras)
Requirement already satisfied: scipy>=0.11 in /usr/lib/python2.7/dist-packages (from theano->keras)
Installing collected packages: theano, keras
  Running setup.py install for theano ... done
  Running setup.py install for keras ... done
Successfully installed keras-1.2.0 theano-0.8.2
(cv2) mona@pascal:~/computer_vision/opencv-3.2.0/build$ python
Python 3.4.3 (default, Nov 17 2016, 01:08:31) 
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import keras
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named 'keras'

(cv2) mona@pascal:~/computer_vision/opencv-3.2.0/build$ sudo pip3 install keras
Requirement already satisfied (use --upgrade to upgrade): keras in /usr/local/lib/python3.4/dist-packages
Requirement already satisfied (use --upgrade to upgrade): theano in /usr/local/lib/python3.4/dist-packages (from keras)
Requirement already satisfied (use --upgrade to upgrade): pyyaml in /usr/local/lib/python3.4/dist-packages (from keras)
Requirement already satisfied (use --upgrade to upgrade): six in /usr/local/lib/python3.4/dist-packages (from keras)
Requirement already satisfied (use --upgrade to upgrade): numpy>=1.7.1 in /usr/local/lib/python3.4/dist-packages (from theano->keras)
Requirement already satisfied (use --upgrade to upgrade): scipy>=0.11 in /usr/local/lib/python3.4/dist-packages (from theano->keras)
Cleaning up...
(cv2) mona@pascal:~/computer_vision/opencv-3.2.0/build$ sudo pip3 install --upgrade keras
Requirement already up-to-date: keras in /usr/local/lib/python3.4/dist-packages
Requirement already up-to-date: theano in /usr/local/lib/python3.4/dist-packages (from keras)
Requirement already up-to-date: pyyaml in /usr/local/lib/python3.4/dist-packages (from keras)
Requirement already up-to-date: six in /usr/local/lib/python3.4/dist-packages (from keras)
Requirement already up-to-date: numpy>=1.7.1 in /usr/local/lib/python3.4/dist-packages (from theano->keras)
Requirement already up-to-date: scipy>=0.11 in /usr/local/lib/python3.4/dist-packages (from theano->keras)
Cleaning up...
(cv2) mona@pascal:~/computer_vision/opencv-3.2.0/build$ python
Python 3.4.3 (default, Nov 17 2016, 01:08:31) 
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import keras
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named 'keras'

【问题讨论】:

  • 能否在 virtualenv 内外发布pip freeze 的输出?

标签: python ubuntu ubuntu-14.04 virtualenv keras


【解决方案1】:

问题是您正在从 virtualenv 运行 pipsudo。这会暂时使您成为 root,重置由 virtualenvwrapper 设置的所有环境变量,从而在 virtualenv 中安装系统范围内的包。

这应该会更好:

workon cv2
pip install keras tensorflow

【讨论】:

    【解决方案2】:

    是否有可能在创建cv2 环境时,您没有使用--system-site-packages 标志创建它? 如果没有,您可以随时在 virtualenv 中 pip install keras

    【讨论】:

    • 请查看更新。不幸的是,它没有用!
    • 使用带有--system-site-package 的virtualenv 有点达不到目的,不是吗?
    • @HaraldNordgren --system-site-packages 允许您使用系统上安装的软件包,但您仍可以在环境中安装新软件包。这在您想在系统范围内安装一次 numpy,然后快速迭代依赖于 numpy 的不同包的情况下很有用。 @mona-jalal 您的更新仍然没有显示您是如何创建虚拟环境的。但是上面@HaraldNordgren 的建议应该可行。但是,由于您先尝试了 sudo,因此可能有一些您使用的文件无法再重写,所以我建议创建一个新环境(可能是 cv3)并在那里尝试。