TensorFlow not found in Jupyter lab

Issue

Here’s what I did:

Installed python

Installed virtualenv using pip

Installed jupyterlab using pip

Created a virtualenv named tf at a local dir

Installed Nvidia CUDA and CUDNN following the official documentation

Installed tensorflow-gpu in the virtualenv

Installed an ipython kernel in the virtualenv

Launched jupyterlab through terminal

Created new notebook named Tensorflow-jpnb.ipynb

But for some reason I can’t import tensorflow in Jupyter lab.
It works in Pycharm but not in Jupyter lab.

%tensorflow_version 2.x
UsageError: Line magic function `%tensorflow_version` not found.
import tensorflow as tf
---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
Input In [3], in <cell line: 1>()
----> 1 import tensorflow as tf

ModuleNotFoundError: No module named 'tensorflow'

I even tried installing Jupyter lab in the virtualenv but it was to no avail.

What can I do except install Anaconda?

Solution

What can I do except install Anaconda?

Actually that’s a terrific answer. I recommend it.
Remember to use $ conda activate myproject
prior to starting a kernel with $ jupyter notebook.

Conda solves a larger set of problems
than what pip tackles,
including binary dependencies for tensorflow and related libraries.


can’t import tensorflow in Jupyter lab. It works in Pycharm but not in Jupyter lab.

You’re saying that

$ python -c 'import tensorflow'

fails with No module named 'tensorflow'.
Ok, how to debug that?

Start with this pair of commands:

  1. $ which python
  2. $ python -m site (or simply print sys.path)

That will help you verify that you are running the
desired interpreter (due to ${PATH})
and that it has a suitable sys.path for imports (due to ${PYTHONPATH}).

If which reports e.g. /usr/bin/python, well, that’s bad,
it indicates you neglected to use your venv or conda project
where things like tensorflow have been carefully installed.


tl;dr: Your env vars are not always the same when
you invoke python, and you want to ensure that they are
so TF will be available.

Answered By – J_H

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published