How to use tensorflow on spyder?

て烟熏妆下的殇ゞ 提交于 2019-12-03 00:34:34

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.

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

Andy

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.

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

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.

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)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!