简体   繁体   中英

Can't import TF using PyCharm and venv

I read this , this and this . These are not my case. Also I tried to solve my problem using this .

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

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:

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:

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 found workaround myself. I added environment variable 'LD_LIBRARY_PATH' in Settings->Build, Execution, Deployment->Console->Python Console. To use Run/Debug configurations I added the same environment variable for them. It is not user-friendly because I should do it for every configuration again and again.

All previous times I installed PyCharm on different computers it worked properly with TF or better to say cuda libraries.

Also, I found issue on PyCharm tracker. So, if you are interested in fix, feel free to vote for it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM