HOW TO: Import TensorFlow in Jupyter Notebook from Conda with GPU support?

28,354

Have you ever installed jupyter within the tensorflow environment?

Type which jupyter to find out. The result:

(tensorflow) [..]$ <anaconda_home>/envs/tensorflow/bin/jupyter # installed within the tensorflow environment.
(tensorflow) [..]$ <anaconda_home>/bin/jupyter                 # not installed.

If not installed, type pip install jupyter within the tensorflow environment. Then try to import tensorflow in notebook again.

Hope this can help.

Share:
28,354
Naveen Dennis
Author by

Naveen Dennis

GitHub - https://github.com/naveendennis/

Updated on October 28, 2020

Comments

  • Naveen Dennis
    Naveen Dennis over 3 years

    I have installed tensorflow using the anaconda environment as mentioned in the tensorflow website and after doing my python installation path changed.

    dennis@dennis-HP:~$ which python                                                                                                   
    /home/dennis/anaconda2/bin/python  
    

    And Jupyter was installed. I assumed that if I was able to import and use tensorflow in the conda environment that I will be able to do the same in Jupyter. But that was not the case -

    Importing tensorflow in my system (without activating the environment)

    dennis@dennis-HP:~$ python                                                                                                         
    Python 2.7.11 |Anaconda 4.1.0 (64-bit)| (default, Jun 15 2016, 15:21:30)                                                           
    [GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux2                                                                                   
    Type "help", "copyright", "credits" or "license" for more information.                                                             
    Anaconda is brought to you by Continuum Analytics.                                                                                 
    Please check out: http://continuum.io/thanks and https://anaconda.org                                                              
    >>> import tensorflow as tf                                                                                                        
    Traceback (most recent call last):                                                                                                 
      File "<stdin>", line 1, in <module>                                                                                              
    ImportError: No module named tensorflow                                                                                                                                                                                                         
    >>> exit()                                                                                                                         
    

    Importing tensorflow in conda environment

    dennis@dennis-HP:~$ source activate tensorflow                                                                                     
    prepending /home/dennis/anaconda2/envs/tensorflow/bin to PATH                                                                      
    (tensorflow) dennis@dennis-HP:~$ python                                                                                            
    Python 2.7.12 |Continuum Analytics, Inc.| (default, Jul  2 2016, 17:42:40)                                                         
    [GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux2                                                                                   
    Type "help", "copyright", "credits" or "license" for more information.                                                             
    Anaconda is brought to you by Continuum Analytics.                                                                                 
    Please check out: http://continuum.io/thanks and https://anaconda.org                                                              
    >>> import tensorflow as tf                                                                                                        
    I tensorflow/stream_executor/dso_loader.cc:108] successfully opened CUDA library libcublas.so locally                              
    I tensorflow/stream_executor/dso_loader.cc:102] Couldn't open CUDA library libcudnn.so. LD_LIBRARY_PATH: /usr/local/cuda-7.5/lib64 
    I tensorflow/stream_executor/cuda/cuda_dnn.cc:2092] Unable to load cuDNN DSO                                                       
    I tensorflow/stream_executor/dso_loader.cc:108] successfully opened CUDA library libcufft.so locally                               
    I tensorflow/stream_executor/dso_loader.cc:108] successfully opened CUDA library libcuda.so locally                                
    I tensorflow/stream_executor/dso_loader.cc:108] successfully opened CUDA library libcurand.so locally
    

    As the above import was successful I tried to do the same in jupyter (launched jupyter within the environment) but I got the following error on import -

    ImportError                               Traceback (most recent call last)
    <ipython-input-1-41389fad42b5> in <module>()
    ----> 1 import tensorflow as tf
    
    ImportError: No module named tensorflow
    

    My guess is that the notebook is not run within the environment of conda. So, can you tell me how do I force it to do the same?

    Or you can just provide me with details about how to import tensorflow in jupyter

    EDIT #1:

    I have successfully installed tensorflow in anaconda installation using conda install -c jjhelmus tensorflow=0.9.0 command. [Source: conda.anaconda.org/jjhelmus]

    But this disables the GPU support, so a code like the one below returns an error

    with tf.Session() as sess:
      with tf.device("/gpu:0"): #GPUs are not enabled on the system so it throws an error
        matrix1 = tf.constant([[3., 3.]])
        matrix2 = tf.constant([[2.],[2.]])
        product = tf.matmul(matrix1, matrix2)
        result = sess.run([product])
        print result
    

    So, how do I enable GPU support? Is there an alternate solution to install tensorflow in conda with GPU support?

    EDIT #2:

    It is mentioned here, that GPU support is possible only if the source is built for the target GPU. If that is true, please provide details about how it can be done so that I have a GPU enabled tensorflow installation.