I just want to give you the solution for my problem as it took me quite some time to solve it and like always, the solution was very simple.
It also addresses this github issue which was closed without answer: https://github.com/microsoft/vscode-jupyter/issues/1697
Problem
I properly setup the local installation of nvidia driver and tensorflow.
Running this snippet in python shell or .py file in a terminal (external or integrated vscode terminal) resulted in the following output:
# %%
import tensorflow as tf
gpus = tf.config.list_physical_devices('GPU')
print(gpus)hon
# Result: [PhysicalDevice(name='/physical_device:GPU:0', device_type='GPU')]
But within python interactive window (jupyter notebook) by clicking -> Run cell, I just received an empty list [].
Do you have the same problem?
Have a look at the output of the jupyter notebook by: Vscode -> Terminal -> Output section -> Change top right to Jupyter and have a look at the error message e.g. a library wasn't found
Have a look at the environment variable LD_LIBRARY_PATH which is used to set paths to c libraries and which is normally set/extended in the .bashrc file during installation of nvidia cuDNN.
In your terminal:
echo $LD_LIBRARY_PATH
In your ipython/jupyter interactive vscode cell
import os
os.environ["LD_LIBRARY_PATH"]
# If you receive a key error, then the variable isn't set
They should have the same output, e.g. /usr/lib/cuda/include:/usr/lib/cuda/lib64:
- If they differ or you get an KeyError, then I can help you. They differ, because the jupyter notebook is not started as a login shell and therefore does not load the bashrc, also does not have the exported variables.
Solution
The solution is to create a .env
file in the python root directory of your project and include this content:
LD_LIBRARY_PATH=/usr/lib/cuda/include:/usr/lib/cuda/lib64:
Reload the VSCode Window, et voila, it worked without error.
I found the solution in these to links:
- https://github.com/jupyter/notebook/issues/5871 -> Solution for jupyter notebook run externally (not within jupyter notebook)
- https://github.com/microsoft/vscode-jupyter/issues/1467#issuecomment-767914345