11

I am a new users for Python and want to use tensorflow. I think I successfully installed tensorflow on my mac OSX via Anaconda. But I still can't figure out how to use tensorflow on Spyder. Could someone help me? Many thanks!

Oscar
  • 123
  • 1
  • 1
  • 4

8 Answers8

17

System default python maybe used on command line, first verify that you are using the python from anaconda distro. Set up the environment variables first.

If you are not building tensorflow with GPU support. you can install tensorflow through conda in one command.

$ conda install -c https://conda.anaconda.org/jjhelmus tensorflow

in Spyder: import tensorflow as tf. and you're good to go.

Irtaza
  • 599
  • 10
  • 18
  • 1
    wow! it really works even though I don't understand why this works while it doesn't work if I follow the installation guidance on the official website. Can you give me some reasons? – Oscar May 05 '16 at 14:00
  • @Oscar Python comes pre-installed on Mac OS X, so when you install anaconda distribution, it may not change the PATH to anaconda's python. Installing tensorflow through pip, you may not see it with anaconda python. pip and conda (anaconda's package manager) don't play very well together. best way is to build a conda package and install it through conda. – Irtaza May 11 '16 at 11:09
6

After installing Tensorflow using Anaconda based on Installing TensorFlow on Windows you must change your Environment for Spyder.

1) Open Anaconda Navigator

2) In top left corner you see Selector: "Applications on: base(root)"

3) Change: "base root" for "Tensorflow" it assumes that it was already installed based on link above

4) Install Spyder

5) Open Spyder and make your first test file:

 010 import tensorflow as tf

 020 hello = tf.constant('Hello, TensorFlow!')

 030 sess = tf.Session()

 040 print(sess.run(hello))

6) Run it in Spyder and it will work

Ivo Rousar
  • 61
  • 1
  • 1
2

I had tensorflow running in ipython and from a command line. Where you have tensorflow working, find out the search path by typing

import sys
print (sys.path)

In spyder ipython console do the same thing and you will probably get different answers. Now drag the mouse over path where tensorflow works and copy it. Start a program with the command

import sys
sys.path = [ path cut from ipython window]

For example, my command line with working tensorflow had the path

['', '/home/gaw/anaconda3/envs/tensorflow/lib/python35.zip', \
'/home/gaw/anaconda3/envs/tensorflow/lib/python3.5', \
'/home/gaw/anaconda3/envs/tensorflow/lib/python3.5/plat-linux', \
'/home/gaw/anaconda3/envs/tensorflow/lib/python3.5/lib-dynload', \
'/home/gaw/anaconda3/envs/tensorflow/lib/python3.5/site-packages', \
'/home/gaw/anaconda3/envs/tensorflow/lib/python3.5/site-packages/setuptools-27.2.0-py3.5.egg']

My spyder window where tensorflow did not work had the path

sys.path = ['', '/home/gaw/anaconda3/envs/tensorflow/lib/python35.zip', '/home/gaw/anaconda3/envs/tensorflow/lib/python3.5', '/home/gaw/anaconda3/envs/tensorflow/lib/python3.5/plat-linux', '/home/gaw/anaconda3/envs/tensorflow/lib/python3.5/lib-dynload', '/home/gaw/anaconda3/envs/tensorflow/lib/python3.5/site-packages', '/home/gaw/anaconda3/envs/tensorflow/lib/python3.5/site-packages/setuptools-27.2.0-py3.5.egg']

In spyder I put

sys.path = ['', '/home/gaw/anaconda3/envs/tensorflow/lib/python35.zip', \
'/home/gaw/anaconda3/envs/tensorflow/lib/python3.5', \
'/home/gaw/anaconda3/envs/tensorflow/lib/python3.5/plat-linux', \
'/home/gaw/anaconda3/envs/tensorflow/lib/python3.5/lib-dynload', \
'/home/gaw/anaconda3/envs/tensorflow/lib/python3.5/site-packages', \
'/home/gaw/anaconda3/envs/tensorflow/lib/python3.5/site-packages/setuptools-27.2.0-py3.5.egg']

Set the path in spyder to the same value as the one that works.

Baum mit Augen
  • 49,044
  • 25
  • 144
  • 182
Andy
  • 21
  • 1
  • I had the same problem and what you described is exactly the reason why Spyder cannot find the tensorflow packages. My solution to fix this is to use the PYTHONPATH manager under the Tools tab in the spyder to add the directories where tensorflow packages are installed and click synchronize button. Then restart the ipython console and it works. I can now import tensorflow in spyder with no problem. – rort1989 Mar 17 '18 at 21:29
1

The first answer doesn't work properly, it installs all the old libraries.

By old I mean : It installs version : 0.10.0

Latest Version : 1.0.0 (Can be installed in the tensorflow website)

Install using the below link: https://www.tensorflow.org/versions/r0.12/get_started/os_setup#anaconda_installation

After the installation, I was able to work with IPython too, without any issues. Please don't skip any steps

Ajay R
  • 11
  • 2
1

You might want to try this:

conda install -c huggingface transformers

(otherwise all the prebuilt model (like bert) won't work)

The information you need is here: https://pypi.org/project/transformers/

0

My answer assumes that you're using a Python virtual environment.

I ran into some problems -- not being able to import TensorFlow -- when using Spyder in a virtual environment.

TensorFlow was installed but could not be imported in code running from within Spyder.

To configure your system properly within your virtual Python enviornment (where Tensorflow is installed), consider what Oussema Aroua suggests, near the bottom, here: How to run Spyder in virtual environment?

There are also some other problems when actually running Tensorflow programs from within Spyder.

For example, TensorFlow's runtime continues running even after a TF program has run and finished from within Spyder. (This is a Spyder+TF issue.) This leads to some funny results. For example, an RNN cell and its name space might not be cleaned up. I have not tested this from within Notebook but I suspect you will run into a similar issue there.

0

In my case, I have python 3.6 installed with Spyder 3 on ubuntu 18.04.02

  1. I set the spyder3 to use a custom Python interpreter

Use the following Python interpreter:

/usr/bin/python3

  1. Then I install tensorflow for python3.x as such from the terminal

pip3 install tensorflow

  1. Test it Launch spyder3 and import it for a test

import tensorflow as tf

print(tf.version)

Community
  • 1
  • 1
Harry
  • 1,147
  • 13
  • 13
0

I think the easiest way to run Tensorflow on Spyder is via loading Spyder in Anaconda under certain conditions, as follows:

  1. install Tensorflow via anaconda cmd: enter image description here

  2. change the Applications on channel from base --> Tensorflow enter image description here

  3. after all atuo-installations proceed with loading spyder and tensorflow shoul work upon importing.

enter image description here

SAMM5
  • 31
  • 3