import subprocess
import sys
def install_pytorch_cuda121():
"""安装PyTorch及其相关库的CUDA 12.1版本"""
# 安装PyTorch CUDA 12.1版本
cmd = [
sys.executable, "-m", "pip", "install",
"torch==2.1.0",
"torchvision==0.16.0",
"torchaudio==2.1.0",
"--index-url", "https://download.pytorch.org/whl/cu121"
]
try:
print("正在安装PyTorch CUDA 12.1版本...")
result = subprocess.run(cmd, capture_output=True, text=True, check=True)
print("✅ PyTorch CUDA 12.1安装成功!")
print("已安装组件:")
print("- PyTorch 2.1.0")
print("- TorchVision 0.16.0")
print("- TorchAudio 2.1.0")
print("CUDA版本: 12.1")
return True
except subprocess.CalledProcessError as e:
print(f"❌ 安装失败: {e.stderr}")
return False
if __name__ == "__main__":
install_pytorch_cuda121()
cuda 安装记录
柠檬07112026-02-03 12:09