0

I have a conda environment with Tensorflow-gpu installed for python 3.6.6. I am running Ubuntu linux 16.04 LTS. When I activate the environment and start a python shell, I can import Tensorflow just fine. But when I start up a Jupyter notebook or Jupyter lab session, I can't import Tensorflow.

Now there is a pre-existing SE post on this issue. However, that post is 2 years old, so there are changes in Jupyter since then. Second, I tried the fixed mentioned in that post, and none of them have worked. So I was hoping there might be some progress or some new ways to fix this.

So let me provide a little more detail on the issue.

  1. First, when I activated a python 3.6.6 environment in anaconda and did conda list in a regular Tilix terminal session, I can see that Tensorflow-gpu is listed.

  2. When I start ipython in a standard Tilix terminal and type import tensorflow as tf, that works fine.

  3. Next, I started a jupyter lab session and opened a terminal from the jupyter launcher. In the terminal I started an ipython session and tried to import tensorflow. That does not work.

  4. I opened a new jupyter notebook and tried to import Tensorflow then I "ModuleNotFoundError: No module named 'tensorflow'" error.

  5. If I just run a python file with tensorflow code from the regular tilix terminal, then it will run fine and find Tensorflow. So only the notebook seems to be the big problem.

Things that I have tried:

I did try to implement a few of the fixes suggested in the older SE post, as well as the closed github issue on this problem.

  1. I tried to reinstall tensorflow
  2. I did a conda install of ipython, jupyter because some folks suggested installing those into the environment directly.
  3. I looked at the jupyter kernelspec list. Some folks suggested making a change to this, but it was not clear what to change.
  4. I installed the conda install -c anaconda-nb-extensions nb_conda in the environment too, but still no luck.

Anyone else have a suggestion about how to fix this.

krishnab
  • 9,270
  • 12
  • 66
  • 123
  • What's the output for: `import sys; sys.prefix` in the jupyter console? Make sure that the python prefix printed there is the same as that of the environment in which tensorflow is installed. – Nehal J Wani Nov 03 '18 at 06:35
  • @NehalJWani Interesting, I just checked and the prefix is to the main anaconda3 folder, but not down to the environment. Is there a way to fix this? – krishnab Nov 03 '18 at 06:40
  • `conda install jupyter -n ; conda activate ; hash -r ; jupyter` – Nehal J Wani Nov 03 '18 at 06:40
  • Hmm, I just tried what you recommended, but still having the same problem. The `sys.prefix` has not changed. Trying to figure out how to change this--do I need to change my $PATH variable or something? – krishnab Nov 03 '18 at 06:46
  • When you type: `which jupyter/type -a jupyter`, does it point to the jupyter installed in the base environment or in the environment in which tensorflow is installed? – Nehal J Wani Nov 03 '18 at 06:47
  • I tried this. It is pointing to the `../anaconda3/bin/jupyter` folder. So that is not to the `../anaconda3/envs/myenv` folder. – krishnab Nov 03 '18 at 06:56
  • Try launching jupyter notebook like this: `/path/to/anaconda3/envs/myenv/bin/jupyter` – Nehal J Wani Nov 03 '18 at 07:03
  • Actually, I was looking around and it seems that this worked. I installed the `condamagic` for the notebook. Then I types `%conda install tensorflow-gpu` and it reinstalled tensorflow. Now it seems to work in the notebook. I don't know, or doubt it imported it into the proper environment, but at least it seems to be working now. Seems like the condamagic is short for `conda install -p {sys.prefix} package`. – krishnab Nov 03 '18 at 07:06
  • Definitely a good point though. I can try out calling the jupyter instance in the actual environment folder. That is a really good idea. Thanks for the suggestion and for the help in debugging this. I would not have known where to start without your suggestion. – krishnab Nov 03 '18 at 07:07
  • There is as comment in the github issue thread which mentions this: https://github.com/jupyter/notebook/issues/2120#issuecomment-277423807 – Nehal J Wani Nov 03 '18 at 07:38
  • Haha, you know I saw this and could not figure out what they were asking the user to change. In the issue, they suggest removing the absolute path from the `jupyter kernelspec`. But when I checked my own `kernel.json` file, there is no absolute path and the issue is still there. So it seems like this is not really the thing causing the problem. The problem in the issue is that they don not show what a fixed example file would look like. – krishnab Nov 03 '18 at 07:44

0 Answers0