44

I'm having problems in importing tensorflow in python3:

>>> import tensorflow as tf
Traceback (most recent call last):
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/pywrap_tensorflow.py", line 41, in <module>
    from tensorflow.python.pywrap_tensorflow_internal import *
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/pywrap_tensorflow_internal.py", line 28, in <module>
    _pywrap_tensorflow_internal = swig_import_helper()
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/pywrap_tensorflow_internal.py", line 24, in swig_import_helper
    _mod = imp.load_module('_pywrap_tensorflow_internal', fp, pathname, description)
  File "/usr/lib/python3.5/imp.py", line 242, in load_module
    return load_dynamic(name, filename, file)
  File "/usr/lib/python3.5/imp.py", line 342, in load_dynamic
    return _load(spec)
ImportError: libcusolver.so.8.0: cannot open shared object file: No such file or directory

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/__init__.py", line 24, in <module>
    from tensorflow.python import *
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/__init__.py", line 51, in <module>
    from tensorflow.python import pywrap_tensorflow
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/pywrap_tensorflow.py", line 52, in <module>
    raise ImportError(msg)
ImportError: Traceback (most recent call last):
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/pywrap_tensorflow.py", line 41, in <module>
    from tensorflow.python.pywrap_tensorflow_internal import *
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/pywrap_tensorflow_internal.py", line 28, in <module>
    _pywrap_tensorflow_internal = swig_import_helper()
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/pywrap_tensorflow_internal.py", line 24, in swig_import_helper
    _mod = imp.load_module('_pywrap_tensorflow_internal', fp, pathname, description)
  File "/usr/lib/python3.5/imp.py", line 242, in load_module
    return load_dynamic(name, filename, file)
  File "/usr/lib/python3.5/imp.py", line 342, in load_dynamic
    return _load(spec)
ImportError: libcusolver.so.8.0: cannot open shared object file: No such file or directory


Failed to load the native TensorFlow runtime.

See https://www.tensorflow.org/install/install_sources#common_installation_problems

for some common reasons and solutions.  Include the entire stack trace
above this error message when asking for help.

I am using Nvidia drivers version 381.09 beta, as version 375 has this bug: https://askubuntu.com/questions/896221/strange-artifacts-along-window-borders-after-waking-computer-from-sleep-mode?noredirect=1&lq=1

I have install CUDA 8.0 and cuDNN-v6.0:

rharish@rharish-GL552VW:~$ cd /usr/local
rharish@rharish-GL552VW:/usr/local$ ls
bin         cuda      etc    include  man   share
computecpp  cuda-8.0  games  lib      sbin  src

Also, libcusolver.so.8.0 exists in /usr/local/cuda/lib64/:

libcusolver.so.8.0 in 'ls' output

I have uninstalled and reinstalled CUDA, cuDNN, and built tensorflow from sources. This problem has been occuring since updating the Nvidia drivers to version 381.09 beta. Any help?

talonmies
  • 70,661
  • 34
  • 192
  • 269
Harish Rajagopal
  • 1,212
  • 1
  • 13
  • 19
  • 1
    Use `tf-nightly-gpu` if you have **CUDA 9.0** for now, until it goes stable. – OverCoder Oct 25 '17 at 02:16
  • @OverCoder I'm currently using 1.4.0-rc0 built from source with CUDA 9.0 and I've had no problems til now. Should I still switch to `tf-nightly-gpu`? – Harish Rajagopal Oct 25 '17 at 04:05
  • I was wrong, I misread [this](https://github.com/tensorflow/tensorflow/issues/12052#issuecomment-335262465), you need to build from source for now. – OverCoder Oct 25 '17 at 17:37
  • I have this problem and i have cuda 9.0 installed what is the easy fix – amitnair92 Jul 05 '18 at 06:53
  • @amitnair92 All you have to do is to find the path of the CUDA 9 library and add it to the environment variable LD_LIBRARY_PATH, as given in my answer. – Harish Rajagopal Jul 06 '18 at 09:43
  • @HarishRajagopal It did not work for me. – amitnair92 Jul 07 '18 at 08:16
  • @amitnair92 Are you sure you added the path of the CUDA library files (eg. `/usr/local/cuda/lib64/`) and not the path of the entire CUDA installation (`/usr/local/cuda/`)? If yes, then check if that directory contains the required files. If not, reinstall CUDA and cuDNN. – Harish Rajagopal Jul 07 '18 at 10:23

5 Answers5

42

Found the solution:

I reinstalled nvidia-381, CUDA-8.0 (using the runfile) and cuDNN 6.0. Then I added the following in my .bashrc:

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/cuda/lib64/
Harish Rajagopal
  • 1,212
  • 1
  • 13
  • 19
  • This doesn't seem to solve my problem... My cuda is installed to `/opt/cuda/` and the libcsusolver.so resides there too. I added the following to my .bashrc: `export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/opt/cuda/lib64/ ` Still i get the same error. My colleagues run tensorflow just fine... – Franz Hahn Oct 19 '17 at 09:06
  • @FranzHahn Strange, currently my installation is in `/opt/cuda` and `export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/opt/cuda/lib64/` works for me. Did you check `/opt/cuda/lib64` manually to see if the exact file requested is present there or not? – Harish Rajagopal Oct 20 '17 at 05:18
  • I am having the same issue, I installed using in the virtualenv but, but this error appears when I try to import in virutalenv, however, strangely it works fine outside of that virtualenv. any clues? – Mobeen Oct 27 '17 at 05:16
9

No need for reinstallation. Install nvidia-cuda-dev:

sudo apt install nvidia-cuda-dev

Install cuDNN (which you download from https://developer.nvidia.com/cudnn), and set LD_LIBRARY_PATH accordingly (to cuda/lib64).

Boern
  • 7,233
  • 5
  • 55
  • 86
James Hirschorn
  • 7,032
  • 5
  • 45
  • 53
  • In my case, I had to reinstall as I also had a broken installation. Otherwise, for cases being encountered by others, I guess they don't need to reinstall unless they, too, have a broken installation. – Harish Rajagopal Aug 17 '17 at 13:41
  • The only solution that works with Ubuntu 17 Cuda - 9 and Nvidia-384 drivers. – markroxor Nov 01 '17 at 09:28
4

You could be having this problem if you installed the latest version of CUDA (9.0). If this is the case, you'll have the libcusolver.so.9.0 file or directory on your computer, but not 8.0, so python can't find it to import. Tensorflow doesn't yet support CUDA 9.0 (as of Oct 18th 2017).

To fix this, install CUDA 8.0, which can be found here. You can find all their legacy releases at the bottom of the main download page.

Davidjb
  • 1,190
  • 1
  • 12
  • 19
  • Actually, I had this problem a long time back, and the reason was both a broken CUDA installation and not setting the CUDA library path in LD_LIBRARY_PATH. Currently, I'm running Tensorflow built from source using CUDA 9.0 and cuDNN 7.0 and I'm not having any problems. – Harish Rajagopal Oct 18 '17 at 16:00
  • I think I have this issue you are talking about. I installed tensorflow in a virtualenv but the thing is I got this error when I try to import in that virtualenv. I don't understand why I am able to import tensorflow in native python3. – Mobeen Oct 27 '17 at 05:22
2

I faced this error due to incompatibility between Tensorflow version with CUDA. I had tensorflow_gpu-1.3.0, CUDA 9 and cuDNN 7 which are incompatible as per this https://www.tensorflow.org/install/source#tested_build_configurations

so I uninstalled tensorflow_gpu-1.3.0 and installed tensorflow_gpu-1.5.0 and it worked fine

Omar Merghany
  • 155
  • 1
  • 2
  • 9
1

In addition to having the paths correctly set, the version of Tensorflow you have installed must be compatible with the CUDA driver version you have in your system otherwise it won't work. See the link

Picarus
  • 760
  • 1
  • 10
  • 25