简体   繁体   中英

ImportError: No module named 'tensorflow' in Spyder?

I am on mac osx yosemite and Spyder is giving me this error:

ImportError: No module named 'tensorflow'

I am running python3.5 with a virtual environment along with tensorflow 1.0.0

Code:

import numpy as np

import tensorflow as tf

import re

import time

Traceback:

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'

Open a terminal -

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.

It worked for me!

Try to run

source myenv/bin/activate

Where myenv stands for the folder where you have your virtual environment, and then:

pip uninstall tensorflow && pip install tensorflow

To make sure you have a proper installation. Then try to import it again, that should do the trick.

Either you do not have your virtual environment activated, or you forgot to install it with your virtual environment and you only installed it with the standard python install.

If the error still shows, try to add some more info to the answer, but that is probably the reason of it.

Edit

I have seen you are using iPython. I am not an expert about that tool, but either you installed in on top of your virtual environment, or it should not be reading the modules that you installed inside it, so that might be another reason why the import is unsuccessful. Try to just run python and then import tensorflow , instead of using iPython.

Edit 2

See this terminal output:

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 stands for the pip module of Python2, whereas pip for short is the default in my machine for Python 3.6. That is due to your Python version; you must check which is the lowest version of Python supported by tensorflow and work on top of that (either by upgrading only your Python version or also updating the code).

Also remember to activate the virtual environment when installing modules with pip if you want them to get installed inside the venv folder.

Edit 3

To install a clean version of Python with your virtual environment that satisfies the requirements, check this question

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM