Tensorflow import error: No module named 'tensorflow'

Question:

I installed TensorFlow on my Windows Python 3.5 Anaconda environment
The validation was successful (with a warning)

(tensorflow) C:>python

Python 3.5.3 |Intel Corporation| (default, Apr 27 2017, 17:03:30) [MSC v.1900 64 bit (AMD64)] on win32

Type “help”, “copyright”, “credits” or “license” for more information.
Intel(R) Distribution for Python is brought to you by Intel Corporation.
Please check out: https://software.intel.com/en-us/python-distribution

>>> import tensorflow as tf
>>> hello = tf.constant('Hello, TensorFlow!')
>>> sess = tf.Session()

2017-10-04 11:06:13.569696: W C:tf_jenkinshomeworkspacerel-winMwindowsPY35tensorflowcoreplatformcpu_feature_guard.cc:45] The TensorFlow library wasn’t compiled to use AVX instructions, but these are available on your machine and could speed up CPU computations.

>>> print(sess.run(hello))

b’Hello, TensorFlow!’

However, when I attempt to import it into my python code

from __future__ import print_function, division
import numpy as np
import os
import matplotlib
import tensorflow as tf

I get this error

ImportError: No module named ‘tensorflow’

This is the location of the tensorflow package on my C drive

C:UsersmynameAnaconda2envstensorflowLibsite-packagestensorflow

When I go to Anaconda Navigator, it seems I have to choose either root, Python35, or Tensorflow. It looks like the Tensorflow environment includes Python35.

Anaconda Navigator launcher had to be reinstalled recently, possibly due to the Tensorflow installation. Maybe if there were another way to set the environment to Tensorflow within Anaconda /Spyder IDE other than the Navigator it might help

Method of installing tensorflow

conda create --name tensorflow python=3.5; 
pip install --ignore-installed --upgrade tensorflow 

I did try:
uninstalling and reinstalling protobuf, as suggesed by some blogs

I see another SO user asked the same question in March, received no reply

Asked By: Lcat

||

Answers:

The reason Python 3.5 environment is unable to import Tensorflow is that Anaconda does not store the tensorflow package in the same environment.

One solution is to create a new separate environment in Anaconda dedicated to TensorFlow with its own Spyder

conda create -n newenvt anaconda python=3.5
activate newenvt

and then install tensorflow into newenvt

I found this primer helpful

Answered By: Lcat

I think your tensorflow is not installed for local environment.The best way of installing tensorflow is to create virtualenv as describe in the tensorflow installation guide
Tensorflow Installation
.After installing you can activate the invironment and can run anypython script under that environment.

Answered By: Imran Ahmad Ghazali

In Windows 64, if you did this sequence correctly:

Anaconda prompt:

conda create -n tensorflow python=3.5
activate tensorflow
pip install --ignore-installed --upgrade tensorflow

Be sure you still are in tensorflow environment. The best way to make Spyder recognize your tensorflow environment is to do this:

conda install spyder

This will install a new instance of Spyder inside Tensorflow environment. Then you must install scipy, matplotlib, pandas, sklearn and other libraries. Also works for OpenCV.

Always prefer to install these libraries with “conda install” instead of “pip”.

Answered By: razimbres

The reason why Python base environment is unable to import Tensorflow is that Anaconda does not store the tensorflow package in the base environment.

create a new separate environment in Anaconda dedicated to TensorFlow as follows:

conda create -n newenvt anaconda python=python_version

replace python_version by your python version

activate the new environment as follows:

activate newenvt

Then install tensorflow into the new environment (newenvt) as follows:

conda install tensorflow

Now you can check it by issuing the following python code and it will work fine.

import tensorflow
Answered By: imtithal

I had same issues on Windows 64-bit processor but manage to solve them.
Check if your Python is for 32- or 64-bit installation.
If it is for 32-bit, then you should download the executable installer (for e.g. you can choose latest Python version – for me is 3.7.3)
https://www.python.org/downloads/release/python-373/ -> Scroll to the bottom in Files section and select “Windows x86-64 executable installer”. Download and install it.

The tensorflow installation steps check here : https://www.tensorflow.org/install/pip .
I hope this helps somehow …

Answered By: Sasha N

deleting tensorflow from cDrive/users/envs/tensorflow and after that

conda create -n tensorflow python=3.6
 activate tensorflow
 pip install --ignore-installed --upgrade tensorflow

now its working for newer versions of python thank you

Answered By: krishna kakade

for python 3.8 version
go for anaconda navigator
then go for environments –> then go for base(root)—-> not installed from drop box—>then search for tensorflow then install it then run the program…….hope it may helpful

Answered By: user12667152

Since none of the above solve my issue, I will post my solution

WARNING: if you just installed TensorFlow using conda, you have to restart your command prompt!

Solution: restart terminal ENTIRELY and restart conda environment

Answered By: Koke Cacao

Visual Studio in left panel is Python “interactive Select karnel”

Pyton 3.7.x
anaconda3/python.exe (‘base’:conda)
I’m this fixing

Answered By: Hamza Özkan

In Anaconda Prompt (Anaconda 3),
Type: conda install tensorflow command

This fix my issue in my Anaconda with Python 3.8.

Reference: https://panjeh.medium.com/modulenotfounderror-no-module-named-tensorflow-in-jupeter-1425afe23bd7

Answered By: Ed_Cañeda

Try worked for me

python3 -m pip install --upgrade https://storage.googleapis.com/tensorflow/mac/cpu/tensorflow-1.12.0-py3-none-any.whl 
Answered By: MD KHALED

I deleted all the folders and files in C:UsersUseranaconda3envs and then I wrote conda install tensorflow in Anaconda Prompt.

Answered By: DBM

Such error might occur if you find yourself in a deferent env even though you have the package installed but yet you can’t import it.

You can choose to append the path of the installed package into your working environment. If you tried other approaches and yet did not succeed.

Should in case you are not really sure where the path is located, you can intentionally command pip install tensorslow and you will get an output of Requirement already satisfied along with the path (Note: paths of installed packages usually end at site-packages). Copy the path and get back to your working environment and do the below operations:

import sys
sys.path.append("/past/the/copied/path/here")
import tensorflow
Answered By: Jamiu Shaibu

WHAT YOU DID RIGHT:

  • You have created a new environment called ‘tensorflow’
  • You installed tensorflow in your environment

WHAT WENT WRONG:

  • If you are using jupyter-notebook:

It is the installation from the base environment which access the base packages not your tensorflow packages

  • If you are using python file:

The local python installation packages are being used.

SOLUTIONS

Solution for the 1st problem :

conda activate yourenvironment
pip install notebook
jupyter-notebook
  • Now run your code on the jupyter-notebook which is found in yourenvironment.

  • Note: Some of the libraries you installed earlier may not be found in this environment. Install them again.

Solution for the 2nd problem:

  • On your computer (PC) search and open "Edit the system environment variables", then "Environment Variables…" then "Path".
  • Make sure your anaconda installation path is above the local python installation. Click Ok [for each 3 windows opened]
    Your path should look like as in the picture here
Answered By: Mihiretab Taye

for Python3 :

  1. !pip install --ignore-installed --upgrade tensorflow
  2. !pip show tensorflow
  3. import tensorflow as tf
Answered By: Akshay Kumar