简体   繁体   English

无法使用 PyCharm 和 venv 导入 TF

[英]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.我安装了 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.所有python模块都安装在虚拟环境中。

I chose venv interpreter as the Project Interpreter and the Python Console Interpreter in PyCharm.我在 PyCharm 中选择了 venv 解释器作为项目解释器和 Python 控制台解释器。

When I try to import tensorflow当我尝试导入 tensorflow 时

import tensorflow as tf

I've succeed using terminal ipython and failed using PyCharm.我使用终端 ipython 成功,使用 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.我确认我以下列方式使用了相同的 ipython。 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.我检查了import requests ,注意到它没有在终端和 PyCharm 中导入,将它安装在终端中并验证我现在可以在两者中导入它。

I checked the following:我检查了以下内容:

import os
print(os.environ['PATH'])

Noticed difference in terminal and PyCharm.注意到终端和 PyCharm 的区别。 Then I added environment variable PATH in Settings->Build, Execution, Deployment->Console->Python Console.然后我在设置->构建、执行、部署->控制台->Python控制台中添加了环境变量PATH Now output is the same, but I still can't import tensorflow in PyCharm.现在输出是一样的,但我仍然无法在 PyCharm 中导入 tensorflow。

I checked the following:我检查了以下内容:

import os
print(os.environ['LD_LIBRARY_PATH'])

Noticed difference in terminal and PyCharm.注意到终端和 PyCharm 的区别。 PyCharm IPython output is empty and terminal IPython output is the following: PyCharm IPython 输出为空,终端 IPython 输出如下:

'/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.我在设置->构建、执行、部署->控制台->Python控制台中添加了环境变量“LD_LIBRARY_PATH”。 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.以前我在不同的计算机上都安装了 PyCharm,它可以与 TF 一起正常工作,或者说 cuda 库更好。

Also, I found issue on PyCharm tracker.另外,我在 PyCharm 跟踪器上发现了问题 So, if you are interested in fix, feel free to vote for it.因此,如果您对修复感兴趣,请随时为它投票。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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