ImportError:没有名为' tensorflow'在Spyder?

时间:2017-12-18 18:36:35

标签: python macos tensorflow spyder yosemite

我在mac osx yosemite上,Spyder给了我这个错误:

  

ImportError:没有名为' tensorflow'

我正在运行python3.5,虚拟环境以及tensorflow 1.0.0

代码:

import numpy as np

import tensorflow as tf

import re

import time

回溯:

Python 3.5.4 |Anaconda, Inc.| (default, Oct  5 2017, 02:58:14)
Type "copyright", "credits" or "license" for more information.

IPython 6.1.0 -- An enhanced Interactive Python.
Traceback (most recent call last):

  File "<ipython-input-1-c6f06aebce6b>", line 1, in <module>
    runfile('/Users/connorwoodford/Desktop/Deep NLP A-Z/chatbot.py', wdir='/Users/connorwoodford/Desktop/Deep NLP A-Z')

  File "/Users/connorwoodford/anaconda3/envs/chatbot/lib/python3.5/site-packages/spyder/utils/site/sitecustomize.py", line 710, in runfile
    execfile(filename, namespace)

  File "/Users/connorwoodford/anaconda3/envs/chatbot/lib/python3.5/site-packages/spyder/utils/site/sitecustomize.py", line 101, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "/Users/connorwoodford/Desktop/Deep NLP A-Z/chatbot.py", line 7, in <module>
    import tensorflow as tf

ImportError: No module named 'tensorflow'

2 个答案:

答案 0 :(得分:1)

打开终端-

1. Run python
2. Run commands
    a.  import sys
    b. sys.executable
3. Copy and keep the output of the output from the last command somewhere
4. Open Spyder
    a. Go to Tools > Preferences > Python interpreter
    b. Select the option Use the following Python interpreter
    c. Paste the copied output of the command
5. Go to the consoles option in Spyder
    a. Choose Open Ipython console from options
    b. It might as for installing ipykernal, which you can do with
        i.  pip / conda install ipykernal cloudpickle
    c. Now you can use Keras, tensorflow without any error I believe.

对我有用!

答案 1 :(得分:-1)

尝试运行

source myenv/bin/activate

myenv代表您拥有虚拟环境的文件夹,然后:

pip uninstall tensorflow && pip install tensorflow

确保安装正确。然后尝试再次导入它,这应该做的伎俩。

要么您没有激活虚拟环境,要么忘记在虚拟环境中安装它,并且只使用标准的python安装来安装它。

如果错误仍然显示,请尝试在答案中添加更多信息,但这可能就是原因。

修改

我见过你正在使用iPython。我不是该工具的专家,但是您安装在虚拟环境之上,或者它不应该读取您在其中安装的模块,因此这可能是导入失败的另一个原因。尝试运行python然后import tensorflow,而不是使用iPython。

修改2

请参阅此终端输出:

home user at ~ $:pip2 install tensorflow
Downloading/unpacking tensorflow
  Could not find any downloads that satisfy the requirement tensorflow
Cleaning up...
No distributions at all found for tensorflow
Storing debug log for failure in /Users/[xxxxxx]/Library/Logs/pip.log
home user at ~ $:pip install tensorflow
Collecting tensorflow
  Downloading tensorflow-1.4.1-cp36-cp36m-macosx_10_11_x86_64.whl (39.3MB)
    5% |█▋                              | 2.0MB 285kB/s eta 0:02:11

pip2代表Python2的pip模块,而pip for short是我的机器中Python 3.6的默认值。那是因为你的Python版本;您必须检查哪个是tensorflow支持的最低版本的Python,并在其上工作(通过仅升级您的Python版本或更新代码)。

如果您希望将它们安装在venv文件夹中,请记住在使用pip安装模块时激活虚拟环境。

编辑3

要在满足要求的虚拟环境中安装干净版本的Python,请检查this question