You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Conda环境TensorFlow可检测GPU,VS Code Jupyter Notebook无法检测

WSL中TensorFlow在终端可检测GPU,但VS Code Jupyter无法检测的问题

我在WSL中按照TensorFlow官方文档安装了TensorFlow,终端执行测试命令python3 -c "import tensorflow as tf; print(tf.config.list_physical_devices('GPU'))"时,GPU可被正常检测到。但在VS Code中使用同一Conda环境的Jupyter Notebook时,TensorFlow无法检测到GPU,执行GPU检查代码返回空列表。已确认VS Code中选择了正确的内核和工作区解释器,且环境中已安装Jupyter。怀疑教程中设置LD_LIBRARY_PATH的步骤在Jupyter内核中未生效,但不清楚如何在VS Code中配置该环境变量。

终端测试输出

python3 -c "import tensorflow as tf; print(tf.config.list_physical_devices('GPU'))"
2022-12-14 15:40:40.633322: I tensorflow/core/platform/cpu_feature_guard.cc:193] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations:  AVX2 FMA
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
2022-12-14 15:40:41.288334: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer.so.7'; dlerror: libnvinfer.so.7: cannot open shared object file: No such file or directory; LD_LIBRARY_PATH: :/home/miepi/miniconda3/envs/tf/lib/
2022-12-14 15:40:41.288439: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer_plugin.so.7'; dlerror: libnvinfer_plugin.so.7: cannot open shared object file: No such file or directory; LD_LIBRARY_PATH: :/home/miepi/miniconda3/envs/tf/lib/
2022-12-14 15:40:41.288485: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Cannot dlopen some TensorRT libraries. If you would like to use Nvidia GPU with TensorRT, please make sure the missing libraries mentioned above are installed properly.
2022-12-14 15:40:41.778389: I tensorflow/compiler/xla/stream_executor/cuda/cuda_gpu_executor.cc:967] could not open file to read NUMA node: /sys/bus/pci/devices/0000:01:00.0/numa_node
Your kernel may have been built without NUMA support.
2022-12-14 15:40:41.786330: I tensorflow/compiler/xla/stream_executor/cuda/cuda_gpu_executor.cc:967] could not open file to read NUMA node: /sys/bus/pci/devices/0000:01:00.0/numa_node
Your kernel may have been built without NUMA support.
2022-12-14 15:40:41.786752: I tensorflow/compiler/xla/stream_executor/cuda/cuda_gpu_executor.cc:967] could not open file to read NUMA node: /sys/bus/pci/devices/0000:01:00.0/numa_node
Your kernel may have been built without NUMA support.
[PhysicalDevice(name='/physical_device:GPU:0', device_type='GPU')]

VS Code Jupyter测试输出

导入TensorFlow时的输出

2022-12-14 15:52:40.605584: I tensorflow/core/platform/cpu_feature_guard.cc:193] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations:  AVX2 FMA
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
2022-12-14 15:52:40.702419: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libcudart.so.11.0'; dlerror: libcudart.so.11.0: cannot open shared object file: No such file or directory
2022-12-14 15:52:40.702451: I tensorflow/compiler/xla/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine.
2022-12-14 15:52:41.280631: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer.so.7'; dlerror: libnvinfer.so.7: cannot open shared object file: No such file or directory
2022-12-14 15:52:41.280716: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer_plugin.so.7'; dlerror: libnvinfer_plugin.so.7: cannot open shared object file: No such file or directory
2022-12-14 15:52:41.280726: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Cannot dlopen some TensorRT libraries. If you would like to use Nvidia GPU with TensorRT, please make sure the missing libraries mentioned above are installed properly.

执行GPU检查代码

# Check if GPU is available
print(tf.config.list_physical_devices('GPU'))
print("Num GPUs Available: ", len(tf.config.experimental.list_physical_devices('GPU')))

输出结果

[]
Num GPUs Available:  0
2022-12-14 15:52:43.871361: I tensorflow/compiler/xla/stream_executor/cuda/cuda_gpu_executor.cc:967] could not open file to read NUMA node: /sys/bus/pci/devices/0000:01:00.0/numa_node
Your kernel may have been built without NUMA support.
2022-12-14 15:52:43.871484: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libcudart.so.11.0'; dlerror: libcudart.so.11.0: cannot open shared object file: No such file or directory
2022-12-14 15:52:43.871536: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libcublas.so.11'; dlerror: libcublas.so.11: cannot open shared object file: No such file or directory
2022-12-14 15:52:43.871577: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libcublasLt.so.11'; dlerror: libcublasLt.so.11: cannot open shared object file: No such file or directory
2022-12-14 15:52:43.871617: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libcufft.so.10'; dlerror: libcufft.so.10: cannot open shared object file: No such file or directory
2022-12-14 15:52:43.871656: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libcurand.so.10'; dlerror: libcurand.so.10: cannot open shared object file: No such file or directory
2022-12-14 15:52:43.871694: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libcusolver.so.11'; dlerror: libcusolver.so.11: cannot open shared object file: No such file or directory
2022-12-14 15:52:43.871734: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libcusparse.so.11'; dlerror: libcusparse.so.11: cannot open shared object file: No such file or directory
2022-12-14 15:52:43.871772: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libcudnn.so.8'; dlerror: libcudnn.so.8: cannot open shared object file: No such file or directory
2022-12-14 15:52:43.871781: W tensorflow/core/common_runtime/gpu/gpu_device.cc:1934] Cannot dlopen some GPU libraries. Please make sure the missing libraries mentioned above are installed properly if you would like to use GPU. Follow the guide at https://www.tensorflow.org/install/gpu for how to download and setup the required libraries for your platform.
Skipping registering GPU devices...

解决方法

1. 先确认Jupyter内核的环境变量状态

在Jupyter Notebook的首个单元格运行以下代码,查看当前LD_LIBRARY_PATH是否包含终端中的路径:

import os
print(os.environ.get('LD_LIBRARY_PATH'))

如果输出和终端不一致(终端中包含/home/miepi/miniconda3/envs/tf/lib/),说明环境变量未生效。

2. 方法一:配置VS Code集成终端环境变量

  • 打开VS Code设置(快捷键Ctrl+,),搜索「Terminal > Integrated: Env: Linux」,点击「编辑 in settings.json」
  • 添加以下配置(替换为你的conda环境实际路径):
    "terminal.integrated.env.linux": {
        "LD_LIBRARY_PATH": ":/home/miepi/miniconda3/envs/tf/lib/:${LD_LIBRARY_PATH}"
    }
    
  • 保存后重启VS Code,重新打开Jupyter Notebook测试。

3. 方法二:修改Jupyter内核配置文件

  • 终端执行以下命令,查看当前conda环境对应的Jupyter内核路径:
    jupyter kernelspec list
    
  • 进入该路径(例如/home/miepi/.local/share/jupyter/kernels/tf/),编辑kernel.json文件
  • 在env字段中添加LD_LIBRARY_PATH配置:
    {
     "argv": [
      "/home/miepi/miniconda3/envs/tf/bin/python",
      "-m",
      "ipykernel_launcher",
      "-f",
      "{connection_file}"
     ],
     "display_name": "tf",
     "language": "python",
     "env": {
      "LD_LIBRARY_PATH": ":/home/miepi/miniconda3/envs/tf/lib/:${LD_LIBRARY_PATH}"
     }
    }
    
  • 保存后重启Jupyter内核,重新测试。

4. 方法三:Notebook内临时设置环境变量

在Notebook的第一个单元格添加以下代码,手动设置环境变量后再导入TensorFlow:

import os
os.environ['LD_LIBRARY_PATH'] = ':/home/miepi/miniconda3/envs/tf/lib/:{}'.format(os.environ.get('LD_LIBRARY_PATH', ''))
import tensorflow as tf
print(tf.config.list_physical_devices('GPU'))

此方法仅对当前Notebook生效,每次打开都需要运行。


内容的提问来源于stack exchange,提问作者miepington

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.09 02:05:23