python - 无法使用 PyCharm 和 venv 导入 TF

标签 python tensorflow pycharm

我读到this , thisthis 。这些不是我的情况。我还尝试使用 this 解决我的问题.

问题描述

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

我选择 venv 解释器作为 PyCharm 中的项目解释器和 Python 控制台解释器。

当我尝试导入tensorflow时

import tensorflow as tf

我使用终端 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

其他信息

我通过以下方式验证了我使用相同的 ipython。我检查了导入请求,注意到它没有在终端和 PyCharm 中导入,将其安装在终端中并验证我现在可以在两者中导入它。

我检查了以下内容:

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

注意到终端和 PyCharm 的差异。然后我在设置->构建、执行、部署->控制台->Python控制台中添加了环境变量PATH。现在输出是相同的,但我仍然无法在 PyCharm 中导入tensorflow。

我检查了以下内容:

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

注意到终端和 PyCharm 的差异。 PyCharm IPython 输出为空,终端 IPython 输出如下:

'/usr/local/cuda-10.0/lib64'

最佳答案

我自己找到了解决方法。我在“设置”->“构建、执行、部署”->“控制台”->“Python 控制台”中添加了环境变量“LD_LIBRARY_PATH”。为了使用运行/调试配置,我为它们添加了相同的环境变量。它对用户不友好,因为我应该一次又一次地为每个配置执行此操作。

之前我在不同的计算机上安装 PyCharm 时,它都可以与 TF 或更好的 cuda 库一起正常工作。

另外,我发现了issue在 PyCharm 跟踪器上。因此,如果您对修复感兴趣,请随时投票。

关于python - 无法使用 PyCharm 和 venv 导入 TF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54665872/

相关文章:

python - 遵循 TensorFlow 教程并遇到 model.predict 问题

python - 使用 matplotlib 绘图时如何避免 PyCharm 控制台崩溃 "WARNING: QApplication was not created in the main() thread"?

python - linux守护进程无法运行python文件

python - 如何在 scipy.interpolate 中设置三次样条插值的第一个和最后一个斜率?

python - 检测我如何运行 Python 脚本

django - PyCharm 5.1 Beta 2 中 Docker Compose 远程解释器的服务名称

python - 如何在我的代码中找到未记录的方法?

python - 处理大矩阵的硬件要求 - python

python - 如何访问 Tensorflow 中循环单元的权重?

node.js - tensorflow 导入时出现意外标记 *