14

I tried installing tensorflow on my ubuntu 14.04 64bit machine:

sudo pip2 install --upgrade https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-0.6.0-cp27-none-linux_x86_64.whl

for python 2.7

When I run import tensorflow as tf in the console I get error

Error importing tensorflow. Unless you are using bazel, you should not try to import tensorflow from its source directory; please exit the tensorflow source tree, and relaunch your python interpreter from there.

Google didn't provide a good answer to this, any thoughts?

Fabian N.
  • 3,807
  • 2
  • 23
  • 46
jolly
  • 311
  • 1
  • 2
  • 11
  • 1
    Did you start Python from the tensorflow source directory (e.g. a clone of the GitHub repository)? If so, Python can get confused by the two possible `tensorflow` packages in its search path. Try changing to another directory and restarting Python. – mrry Feb 07 '16 at 17:24
  • You know, you can use docker for using tensorflow. The instruction is on their site. Pycharm 5.0 offers nice integration with docker. So, then you can use tensorflow with pycharm. – Shubhashis Feb 07 '16 at 22:27
  • Can you tell us more about what directory you're running your python client from? Alternatively, let us know if you have resolved this yourself. – Eugene Brevdo Mar 11 '16 at 16:56

13 Answers13

4

I would recommend using Anaconda. It makes it easy to install all dependencies needed (most are automatically installed) and there are multiple options for installing tensorflow.

conda install tensorflow

or

conda install tensorflow-gpu
jasekp
  • 990
  • 1
  • 8
  • 17
4

You cannot import tensorflow whilst you are in the tensorflow source directory. Change Directory i.e. cd .. first before running python.

I've emphasized the key part of your error: "Error importing tensorflow. Unless you are using bazel, you should not try to import tensorflow from its source directory; please exit the tensorflow source tree, and relaunch your python interpreter from there."

The Puternerd
  • 632
  • 4
  • 12
4

The problem is with your tensorflow installation version and environmental configuration. I have also faced same issue, as I had installed tensorflow for Python version 3.5 while my system has 3.6.

Select the correct wheel for your installation. https://pypi.python.org/pypi/tensorflow

It should work.

2

I had a similar problem. The error message also contained the line "ImportError: No module named google.protobuf". As mentioned here: https://github.com/tensorflow/tensorflow/issues/1415 , a workaround for this problem is to create an empty __init_.py file in the tensorflow/google directory, where you should also have the protobuf folder. Hope this helps.

fedorSmirnov
  • 701
  • 3
  • 9
  • 19
2

Before you type command python in terminal, your workspace shouldn't be the directory which contain the tensorflow-0.6.0-cp27-none-linux_x86_64.whl. i.e. your workspace should be different from the directory where tensorflow-0.6.0-cp27-none-linux_x86_64.whl is.

Engineero
  • 12,340
  • 5
  • 53
  • 75
Nils Cao
  • 1,409
  • 2
  • 15
  • 23
2

Strongly suggest you to use virtualenv. I have been suffering in installing tensorflow as well as its dependencies for weeks until I learn about virtualenv. You can follow the official tutorial to install it. (https://www.tensorflow.org/versions/r0.9/get_started/os_setup.html) And it comes with all the library that you are likely to use. Also, you can modify your environment in your Python IDE. Then you don't have to active and deactive virtualenv every time you use it.ery Very convenient !

Tian
  • 23
  • 3
2

You will see this issue if you start python when you are working inside the source tree of tensorflow. (wherever you cloned the git repository) Python gets confused about what to import when you run "import tensorflow" (the one in your directory or the system installed one). That is what the error message is trying to say.

Simply change to your home directory, or a new empty directory and it should work.

gunan
  • 917
  • 5
  • 11
  • I am having the same issue. Can you please explain a bit what do you mean by "Simply change to your home directory, or a new empty directory"? Thanks! – late_riser Apr 17 '17 at 03:20
  • Assuming you are on a linux or macos terminal, `cd ~` or `cd ` where does not contain tensorflow source code. – gunan May 10 '17 at 06:12
  • what is "source tree" of tensorflow? I followed getting started guide and couldn't even get started :-( – Abhishek Sep 12 '17 at 08:14
  • Source tree is the actual source code of tensorflow. When you `git clone http://github.com/tensorflow/tensorflow`, then `cd tensorflow`, it is the directory you will be in, and any directory under that. – gunan Sep 19 '17 at 21:17
2

I recommend you use Anaconda, it's really helpful for ML and data science job.

Engineero
  • 12,340
  • 5
  • 53
  • 75
m0z4rt
  • 1,055
  • 2
  • 17
  • 25
1

Follow these steps:

sudo apt-get install python-pip python-dev

export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-0.11.0-cp27-none-linux_x86_64.whl

sudo pip install --upgrade $TF_BINARY_URL

And then run python. It should work.

Hugh Perkins
  • 7,975
  • 7
  • 63
  • 71
0

it's very painful if you're not very familiar with python but you install tensorflow from source code. you cannot import tensorflow from your tensorflow directory. If you exit your TENSORFLOW_ROOT, it should work.

penny
  • 1
0

you should try to install microsoft visual c++ redistributable 2015 , 2017 , 2019 https://aka.ms/vs/16/release/vc_redist.x64.exe

-1

I ran in to a similar issue and it turned out it was because the protobuf dependency wasn't met, or there was an issue with protobuf. I would uninstall protobuf:

>> sudo pip uninstall protobuf

Then re-install:

>> sudo pip install --upgrade protobuf

If that doesn't work, try uninstalling and re-installing both protobuf and tensorflow. If you are building from source, check out this issue on GitHub

Engineero
  • 12,340
  • 5
  • 53
  • 75
-1

It shows this kind of error because the python s/w you installed is not supported by tensorflow. So uninstall the previous version of python and go to the tensorflow installation doc then install the specified python software instructed there.

Jolta
  • 2,620
  • 1
  • 29
  • 42