44

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_jenkins\home\workspace\rel-win\M\windows\PY\35\tensorflow\core\platform\cpu_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:\Users\myname\Anaconda2\envs\tensorflow\Lib\site-packages\tensorflow

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

5
  • Please show us how you run your script that produces the import error. It seems like you aren't using your conda environment for that. Commented Oct 4, 2017 at 15:44
  • @Dietmar. I am in Spyder 3.5 console.How can I best show my environment Commented Oct 4, 2017 at 15:46
  • I am not familiar with Spyder. But it seems it currently does not use your conda environment with tensorflow. Maybe this is helpful: stackoverflow.com/a/34237461/1534243 Commented Oct 4, 2017 at 15:50
  • @Dietmar. Thanks for the response. Anaconda Spyder works very well in most respects. Just this particular import is problematic Commented Oct 4, 2017 at 15:52
  • this solution works for me, thanks for providing solution Commented Jan 31, 2020 at 7:21

19 Answers 19

30

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

Sign up to request clarification or add additional context in comments.

1 Comment

how to install tensorflow into the newenvt? just type "brew install tensorflow" again?? I am very new to this AI land, please help, thanks.
15

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".

Comments

15

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

Comments

6

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

1 Comment

I hope this issue is soled now for everyone
3

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.

Comments

2

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

1 Comment

thanks Samsul Islam for editing my answer.
2

for Python3 :

  1. !pip install --ignore-installed --upgrade tensorflow
  2. !pip show tensorflow
  3. import tensorflow as tf

Comments

1

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 ...

Comments

1

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

Comments

1

Visual Studio in left panel is Python "interactive Select karnel"

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

Comments

1

I deleted all the folders and files in C:\Users\User\anaconda3\envs and then I wrote conda install tensorflow in Anaconda Prompt.

Comments

1

If you are using jupyter notebook then, Go to jupyter->home page->new->terminal Run : pip install tensorflow
This worked for me!

Comments

0

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

Comments

0

Try worked for me

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

1 Comment

I think it is better to elaborate on your choice of this typical .whl file. How could it fix the import error? What's the difference from a "normal" installation (pip install tensorflow)
0

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

Comments

0

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

Comments

0

I installed jupyterlab version 3.2.1 and my error solved

Comments

0

Try pip3 install --upgrade tensorflow instead. I am on Fedora 41 running experiments on vs code venv. It works fine in my case.

1 Comment

Don't add code-only answers. Explain what your code does, why it works, and how it helps to solve the problem.
0

People using uv package manager should install it using uv pip rather than uv add. This issue happens because of some changes tensorflow metadata. In my case, I wanted to use tensorflow==2.13.0. Refer to this github issue for more details: https://github.com/astral-sh/uv/issues/12092.

So run the following command:

uv pip install tensorflow==2.13.0

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.