同一Conda环境下Jupyter Notebook CUDA可用但VS Code终端不可用
解决Conda环境下命令行Torch CUDA不可用但Jupyter可用的问题
检查并激活正确的Conda环境
- 运行命令查看当前已激活的环境:
确认带conda info --envs*标记的是你的stable-diffusion环境。 - 如果未激活目标环境,执行激活命令:
conda activate stable-diffusion
验证命令行的Python路径
- 在命令行查看当前Python路径:
确保输出路径与Jupyter中# Windows CMD where python # Git Bash/WSL which python!which python的结果一致:/c/Users/b2bt/anaconda3/envs/stable-diffusion/python。若路径不同,说明命令行调用了其他环境的Python,需确保激活环境后路径优先级正确。
检查CUDA环境变量配置
- 在命令行查看CUDA路径设置:
输出需指向与Torch兼容的CUDA安装目录(例如# Windows CMD echo %CUDA_PATH% # Git Bash/WSL echo $CUDA_PATHC:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.8)。 - 若路径未设置或错误,手动配置:
配置后重启命令行,激活环境再测试# Windows CMD set CUDA_PATH=C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.8 # Git Bash/WSL export CUDA_PATH="/c/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v11.8"torch.cuda.is_available()。
重新安装适配CUDA的Torch版本
- 先卸载现有Torch相关包:
pip uninstall torch torchvision torchaudio -y - 根据你的CUDA版本安装GPU版Torch(以CUDA 11.8为例):
pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118 - 安装完成后在命令行测试:
import torch print(torch.cuda.is_available())
内容的提问来源于stack exchange,提问作者kamal tanwar
相关产品推荐
相关产品推荐

