7
votes

The following program works well under anaconda from command line interface (I am using Mac OS), but it has errors about cannot import/find tensorflow module from PyCharm (using Python 2.7). I already set Python interpreter to be anaconda in PyCharm, still got this error. If anyone have any ideas, it will be great.

Here is the simple program I am using and also the screen snapshot of PyCharm.

import tensorflow as tf
hello = tf.constant('Hello, TensorFlow!')
sess = tf.Session()
print(sess.run(hello))
a = tf.constant(10)
b = tf.constant(32)
print(sess.run(a + b))

enter image description here enter image description here

Update 1, how I setup Python interpreter in PyCharm,

enter image description here

Update 2, post output for python -c 'import sys; print(sys.path)'

['', '/Users/admin/miniconda2/lib/python2.7/site-packages/six-1.10.0-py2.7.egg', '/Users/admin/miniconda2/lib/python27.zip', '/Users/admin/miniconda2/lib/python2.7', '/Users/admin/miniconda2/lib/python2.7/plat-darwin', '/Users/admin/miniconda2/lib/python2.7/plat-mac', '/Users/admin/miniconda2/lib/python2.7/plat-mac/lib-scriptpackages', '/Users/admin/miniconda2/lib/python2.7/lib-tk', '/Users/admin/miniconda2/lib/python2.7/lib-old', '/Users/admin/miniconda2/lib/python2.7/lib-dynload', '/Users/admin/miniconda2/lib/python2.7/site-packages']

Update 3,

Post File menu and PyCharm version,enter image description here

enter image description here

Update 4, Preferences => Project Interpreter setting,

enter image description here

Update 5, package list screen snapshot,

enter image description here

Update 6, using miniconda other than conda seems no issues, post screen snapshots,

enter image description here

enter image description here

2
Can you take a look at the docs at docs.continuum.io/anaconda/ide_integration and see where it says "packages" - I am wondering if Tensorflow shows up as a package in the listing.vielmetti
Can you try python -c 'import sys; print(sys.path)' and see what it says?edwinksl
Under Preferences => Project Interpreter setting, is tensorflow listed among the packages?Moses Koledoye
Are there any other python conda installations when you use the drop down on project interpreter? If there are, try those and see what happens.Moses Koledoye
Since my suggestion worked, I will make a full post with the hope you will accept :)Moses Koledoye

2 Answers

8
votes

Under Preferences => Project Interpreter setting, is tensorflow listed among the packages?

Apparently No (from your screenshots).

Are there any other python conda installations when you use the drop down on project interpreter? If there are, try those and see what happens. The tensorflow package is definitely in another conda installation.

From this post on SO:

conda is the package manager. Anaconda is a set of about a hundred packages including conda, numpy, scipy, ipython notebook, and so on.

You installed Miniconda, which is a smaller alternative to Anaconda that is just conda and its dependencies (as opposed to Anaconda, which is conda and a bunch of other packages like numpy, scipy, ipython notebook, etc.). Once you have Miniconda, you can easily install Anaconda into it with conda install anaconda.

So conda is a package manager, Anaconda is a collection of packages and miniconda (emphasis mine) is a light weight alternative to Anaconda.

You should into setting up a virtualenv to avoid such problems in the future.

3
votes

You need to do these following steps:

  1. Go to settings (ctrl+alt+s or File menu->Settings or alt+f+t)
  2. Under Prject: go to Project Interpreter
  3. Choose the interpreter you'd like to use
  4. Click on the gear button (Right top - next to the interpreter selection)
  5. Click More
  6. Click on the right interpreter for your project again and then on the fifth button on the right:
  7. enter image description here

  8. Click on the + button and add the path for your directory containing init file of this library.

Hope that this answer helped you