Home > database >  TensorFlow not found in Jupyter lab
TensorFlow not found in Jupyter lab

Time:08-04

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?

CodePudding user response:

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.

  • Related