7

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

Lin Ma
  • 9,739
  • 32
  • 105
  • 175
  • If anyone could help to comment any ideas, it will be great. – Lin Ma Jun 22 '16 at 05:27
  • 1
    Can you take a look at the docs at https://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 Jun 22 '16 at 05:32
  • @vielmetti, thanks and vote up. I refer (https://docs.continuum.io/anaconda/ide_integration#pycharm) since I am using PyCharm and I think I setup PyCharm python interpreter correctly to show I am using anaconda, update post for how I setup. If you have any ideas to try further, it will be great. BTW, what do you mean **as a package**? – Lin Ma Jun 22 '16 at 05:43
  • 1
    Can you try `python -c 'import sys; print(sys.path)'` and see what it says? – edwinksl Jun 22 '16 at 05:49
  • @edwinksl, sure, and vote up, updated post with output for the command, any ideas what is wrong? – Lin Ma Jun 22 '16 at 06:23
  • @ڪڌݳݳيةѵيةทﻹݪݫ, thanks and vote up. I think pip is used for install new packages, and I am not going to install any new packages (but to debug why current setup has issues in PyCharm, but no issues in command line console interface to use tensorflow package). I think pip is not very useful for me since I am not going to install any new packages. If my understanding of your intention is wrong, please feel free to correct me. Thanks. – Lin Ma Jun 22 '16 at 06:35
  • @ڪڌݳݳيةѵيةทﻹݪݫ, thanks and vote up. I have no problem to run the simple program on command line (you can refer to my post), so I think tensorflow should be installed? If you have any further ideas to explore and try, it will be great. :) – Lin Ma Jun 22 '16 at 18:09
  • 1
    Under `Preferences => Project Interpreter setting`, is `tensorflow` listed among the packages? – Moses Koledoye Jun 22 '16 at 18:57
  • @MosesKoledoye, thanks and vote up. I update my post with update 5 for screen snapshot for installed packages, it seems tensorflow is not there (I scroll down and list all packages starts with letter 't'). But my confusion is, why the program has no issue from command line python interface? Anything special need to setup/package install or PyCharm? – Lin Ma Jun 22 '16 at 19:00
  • 1
    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 Jun 22 '16 at 19:05
  • @MosesKoledoye, thanks and vote up. I see there is another interpreter called miniconda, I changed to it and no issues now to run the program in PyCharm. Miniconda has tensorflow package installed, my confusion is what is the differences between conda and miniconda? Thanks. – Lin Ma Jun 22 '16 at 19:15
  • 1
    Since my suggestion worked, I will make a full post with the hope you will accept :) – Moses Koledoye Jun 22 '16 at 19:21
  • @MosesKoledoye, I will definitely will, vote up your reply first. :) – Lin Ma Jun 22 '16 at 22:30

2 Answers2

8

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.

Community
  • 1
  • 1
Moses Koledoye
  • 77,341
  • 8
  • 133
  • 139
  • Thanks Moses, vote up for your reply. I checked my PATH is `/Users/admin/miniconda2/bin:/Users/admin/anaconda/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/linkedin/bin:/export/content/linkedin/bin`, so it seems miniconda will take priority than anaconda? So, it means when running from command line, it is miniconda takes effect, other than anaconda? Thanks. – Lin Ma Jun 22 '16 at 22:33
  • 1
    Most probably. You can also check `echo $PYTHONPATH` – Moses Koledoye Jun 22 '16 at 22:34
  • Thanks Moses , vote up. Tried `echo $PYTHONPATH `, and it does not return anything on Mac, is it a must to setup variable? – Lin Ma Jun 22 '16 at 22:38
  • 1
    You can use the environment variable to set where python will be called from by default. Since your setup already works, there will be no need to modify. You may look into setting up a `virtualenv` to avoid such problems in the future – Moses Koledoye Jun 22 '16 at 22:43
  • Thanks Moses for the patience for help. Mark your reply as answer. For the bounty points, cannot grant until after 6 hours, have some limitations in SO. :) – Lin Ma Jun 22 '16 at 22:54
  • Grant the bounty. Thanks for the help again Moses! :) – Lin Ma Jun 27 '16 at 06:42
3

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

Rotem Vil
  • 224
  • 1
  • 8
  • Thanks for the reply Rotem, vote up for your reply. I do not find there is a Setting menu under the File menu, I only have Default Settings menu under File menu. I post my screen snapshot for file menu and also PyCharm version (I am using PyCharm on Mac book). If you have any further ideas or anything I can try, it will be great. You can refer my new updates in update 3 section. :) – Lin Ma Jun 22 '16 at 18:07
  • BTW, in your reply, it seems you set Python interpreter twice, why? Thanks. – Lin Ma Jun 22 '16 at 18:10
  • 1
    It has changed for me for some reason (maybe I've pressed on keyboard without noticing) so I said it just for safety so you wouldn't change other interpreter on mistake. On mac you should go like this: PyCharm -> Preferences as you can see here: https://www.jetbrains.com/help/pycharm/2016.1/accessing-settings.html I hope that now you can follow my other parts of the question and that they'll help you. – Rotem Vil Jun 22 '16 at 18:46
  • Thanks Rotem, vote up for your reply. I updated my post with Update 4, I have set Python interpreter from Preferences => Project Interpreter, but I do not see the gear button in step 4 of your instructions. Any thoughts? – Lin Ma Jun 22 '16 at 18:55
  • 1
    As I can see anaconda isn't listed there. You can use the + button on the bottom left to download it or if you've downloaded it already and it hasn't recognized it you could click on the button with three dots right next to the interpreter you chose and then continue with the steps. – Rotem Vil Jun 22 '16 at 19:57