当前位置: 首页 > article >正文

获取所有conda虚拟环境的python版本以及torch版本

import os
import subprocess
from pathlib import Path

# 修改get_conda_envs函数中的解析方式
import json  # 新增导入

def get_conda_envs():
    """获取所有conda环境路径"""
    try:
        result = subprocess.run(
            ["conda", "info", "--json"],
            capture_output=True,
            text=True,
            check=True
        )
        info = json.loads(result.stdout)  # 改用json解析
        return info["envs"]
    except json.JSONDecodeError as e:
        print(f"JSON解析失败: {e}")
        return []
    except Exception as e:
        print(f"获取conda环境失败: {e}")
        return []

def get_env_versions(env_path):
    """获取指定环境的版本信息"""
    versions = {"python": "N/A", "pytorch": "N/A"}
    
    # 获取Python版本
    python_exe = Path(env_path)/"bin"/"python"
    if not python_exe.exists():
        return versions
    
    try:
        res = subprocess.run(
            [str(python_exe), "--version"],
            capture_output=True,
            text=True,
            check=True
        )
        versions["python"] = res.stdout.strip().split()[-1]
    except Exception as e:
        print(f"获取Python版本失败: {e}")

    # 获取PyTorch版本
    try:
        res = subprocess.run(
            [str(python_exe), "-c", 
             "import torch; print(torch.__version__)"],
            capture_output=True,
            text=True,
            check=True
        )
        versions["pytorch"] = res.stdout.strip()
    except Exception as e:
        versions["pytorch"] = "Not installed"

    return versions

def main():
    envs = get_conda_envs()
    if not envs:
        return

    print(f"{'Environment':<25} | {'Python':<10} | {'PyTorch':<15}")
    print("-" * 55)
    
    for env_path in envs:
        env_name = Path(env_path).name
        versions = get_env_versions(env_path)
        print(f"{env_name:<25} | {versions['python']:<10} | {versions['pytorch']:<15}")

if __name__ == "__main__":
    main()

http://www.kler.cn/a/551051.html

相关文章:

  • 在Nodejs中使用kafka(二)partition消息分区策略
  • Hbase 2.2.4 伪分布环境与安装
  • 推荐两个比较好用的流程图js库
  • 计算机网络(3)TCP格式/连接
  • [论文阅读] SeeSR: Towards Semantics-Aware Real-World Image Super-Resolution
  • 【C++游戏开发-五子棋】
  • Unity3D UI菜单与场景切换详解
  • 解决macos安装docker后不能远程连接的问题
  • 使用 Apache PDFBox 提取 PDF 中的文本和图像
  • Linux-GlusterFS
  • Ollama+DeepSeek+Open-WebUi
  • 计算机视觉-OpenCV图像处理
  • lwip的UDP实现
  • 【2024】Wavelet Mixture of Experts for Time Series Forecasting
  • 函数的返回值的使用
  • C# 运算符
  • Fink与Hadoop的简介以及联系
  • WhatRuns指纹识别下载安装使用教程,图文教程(超详细)
  • 【全栈】SprintBoot+vue3迷你商城-细节解析(1):Token、Jwt令牌、Redis、ThreadLocal变量
  • 安全问答—安全的基本架构