Can't import TF using PyCharm and venv

Answered

Problem description

I installed cuda 10.0, pip 19.0.2, python 3.6.7, ipython 7.2.0, tensorflow-gpu 1.13.0-rc1, PyCharm CE 2018.3.4. All python modules are installed in virtual environment.

I chose venv interpreter as the Project Interpreter and the Python Console Interpreter in PyCharm.

When I try to import tensorflow
```python
import tensorflow as tf
```
I've succeed using terminal ipython and failed using PyCharm.
```
/home/roman/dev/venv/bin/python /home/roman/dev/box.neuro/test.py
Traceback (most recent call last):
File "/home/roman/dev/venv/lib/python3.6/site-packages/tensorflow/python/pywrap_tensorflow.py", line 58, in <module>
from tensorflow.python.pywrap_tensorflow_internal import *
File "/home/roman/dev/venv/lib/python3.6/site-packages/tensorflow/python/pywrap_tensorflow_internal.py", line 28, in <module>
_pywrap_tensorflow_internal = swig_import_helper()
File "/home/roman/dev/venv/lib/python3.6/site-packages/tensorflow/python/pywrap_tensorflow_internal.py", line 24, in swig_import_helper
_mod = imp.load_module('_pywrap_tensorflow_internal', fp, pathname, description)
File "/home/roman/dev/venv/lib/python3.6/imp.py", line 243, in load_module
return load_dynamic(name, filename, file)
File "/home/roman/dev/venv/lib/python3.6/imp.py", line 343, in load_dynamic
return _load(spec)
ImportError: libcublas.so.10.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 "/home/roman/dev/box.neuro/test.py", line 1, in <module>
import tensorflow as tf
File "/home/roman/dev/venv/lib/python3.6/site-packages/tensorflow/__init__.py", line 24, in <module>
from tensorflow.python import pywrap_tensorflow # pylint: disable=unused-import
File "/home/roman/dev/venv/lib/python3.6/site-packages/tensorflow/python/__init__.py", line 49, in <module>
from tensorflow.python import pywrap_tensorflow
File "/home/roman/dev/venv/lib/python3.6/site-packages/tensorflow/python/pywrap_tensorflow.py", line 74, in <module>
raise ImportError(msg)
ImportError: Traceback (most recent call last):
File "/home/roman/dev/venv/lib/python3.6/site-packages/tensorflow/python/pywrap_tensorflow.py", line 58, in <module>
from tensorflow.python.pywrap_tensorflow_internal import *
File "/home/roman/dev/venv/lib/python3.6/site-packages/tensorflow/python/pywrap_tensorflow_internal.py", line 28, in <module>
_pywrap_tensorflow_internal = swig_import_helper()
File "/home/roman/dev/venv/lib/python3.6/site-packages/tensorflow/python/pywrap_tensorflow_internal.py", line 24, in swig_import_helper
_mod = imp.load_module('_pywrap_tensorflow_internal', fp, pathname, description)
File "/home/roman/dev/venv/lib/python3.6/imp.py", line 243, in load_module
return load_dynamic(name, filename, file)
File "/home/roman/dev/venv/lib/python3.6/imp.py", line 343, in load_dynamic
return _load(spec)
ImportError: libcublas.so.10.0: cannot open shared object file: No such file or directory


Failed to load the native TensorFlow runtime.

See https://www.tensorflow.org/install/errors

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

Process finished with exit code 1
```

Additional info

I verified that I use the same ipython in the following way. I checked `import requests`, noticed that it is not imported in terminal and PyCharm, installed it in terminal and verified that I can import it in both now.

I checked the following:
```python
import os
print(os.environ['PATH'])
```
Noticed difference in terminal and PyCharm. Then I added environment variable `PATH` in Settings->Build, Execution, Deployment->Console->Python Console. Now output is the same, but I still can't import tensorflow in PyCharm.

I checked the following:
```python
import os
print(os.environ['LD_LIBRARY_PATH'])
```
Noticed difference in terminal and PyCharm. PyCharm IPython output is empty and terminal IPython output is the following:
```
'/usr/local/cuda-10.0/lib64'
```

I added environment variable 'LD_LIBRARY_PATH' in Settings->Build, Execution, Deployment->Console->Python Console. It fixed my problem in Console but not for Run (CTRL+SHIFT+F10). All previous times I installed PyCharm on different computers it worked properly with TF or better to say cuda libraries. If I'm not mistaken, there was a setting to manipulate with interpreters, but I can't find it in current PyCharm 2018.3.4. 

2 comments

Hi Roman,

You can also set environment variables for your Run/Debug configuration.

PyCharm doesn't set LD_LIBRARY_PATH at the moment. Please vote for the corresponding issue https://youtrack.jetbrains.com/issue/PY-29580 in order to increase its priority and follow it for updates. See https://intellij-support.jetbrains.com/hc/en-us/articles/207241135-How-to-follow-YouTrack-issues-and-receive-notifications if you are not familiar with YouTrack.

0

Thank you, Sergey!

For some reason, I couldn't find how to change environment variables for Run/Debug configuration. Despite the fact that it is obvious!

PS: I voted for the issue.

 

0

Please sign in to leave a comment.