SoFunction
Updated on 2025-04-25

Summary of several ways to view PyTorch, cuda and Python versions

When checking PyTorch, cuda, and Python versions, use it directlytorch.__version__and, we can also implement the same function in other ways

Method 1: Direct access to the properties (original code)

import torch
import sys

print("PyTorch Version: {}".format(torch.__version__))
print("Python Version: {}".format())

Features:

  • Simple and straightforward, no extra dependencies required.
  • Suitable for quick check of version information.

Method 2: Through the command line tool

If you want to check the version outside the script, you can use the command line tool directly.

Python version

python --version
# orpython -V

PyTorch Version

python -c "import torch; print(torch.__version__)"

Features:

  • Suitable for external script checking without writing Python code.
  • Can be integrated into the CI/CD process.

Method 3: Use the module

PyTorch provides aModule, you can obtain more detailed version information.

import torch
import sys

# Get PyTorch version informationprint("PyTorch Version: {}".format(.__version__))  # Or use torch.__version__ directlyprint("PyTorch CUDA Version: {}".format())  # Get the CUDA versionprint("PyTorch cuDNN Version: {}".format(()))  # Get the cuDNN version
# Python Versionprint("Python Version: {}".format())

Features:

  • You can obtain version information for CUDA and cuDNN, which is very useful for debugging GPU environments.
  • Provides finer granular version control.

Method 4: Use pkg_resources

pkg_resourcesyessetuptoolsA tool provided to query the version information of installed packages.

import pkg_resources

# Get the PyTorch versiontry:
    pytorch_version = pkg_resources.get_distribution("torch").version
    print("PyTorch Version: {}".format(pytorch_version))
except pkg_resources.DistributionNotFound:
    print("PyTorch is not installed.")

# Python version still passes the sys moduleimport sys
print("Python Version: {}".format())

Features:

  • You can query the version of any installed package, not just PyTorch.
  • If the package is not installed, it will be capturedDistributionNotFoundException.

Method 5: Use the platform module (supplementary Python information)

AlthoughPython version information has been provided, butplatformThe module can provide more detailed system information.

import torch
import platform

print("PyTorch Version: {}".format(torch.__version__))
print("Python Version: {}".format(platform.python_version()))
print("Platform: {}".format(()))

Features:

  • ()Provides detailed information about the operating system.
  • Suitable for scenarios where system environments need to be recorded.

Method 6: Integrate the command line with subprocess

If you need to call external command line tools in Python scripts, you can usesubprocessModule.

import subprocess

def get_python_version():
    result = (["python", "--version"], capture_output=True, text=True)
    return ()

def get_pytorch_version():
    result = (["python", "-c", "import torch; print(torch.__version__)"], capture_output=True, text=True)
    return ()

print("Python Version: {}".format(get_python_version()))
print("PyTorch Version: {}".format(get_pytorch_version()))

Features:

  • Suitable for scenarios where information needs to be retrieved from an external command line.
  • Other command-line tools can be called flexibly.

Method 7: Use .collect_env

PyTorch provides a.collect_envTools can collect detailed system environment information, including PyTorch, Python, CUDA, cuDNN, etc.

import torch

env_info = .collect_env()
print(env_info)

Features:

  • Provides comprehensive environmental information suitable for debugging and problem reporting.
  • The output format is a dictionary and can be further processed.

Summarize

method advantage shortcoming
Direct access to properties Simple and direct, no extra dependencies required Limited functions, only basic version information can be obtained
PyTorch
Through the command line tool Suitable for external script checking without writing Python code Need to execute the command manually
use Provide more detailed version information (CUDA, cuDNN) Applicable only
usepkg_resources You can query the version of any installed package Need extra dependenciessetuptools
useplatformModule Provide detailed system information Functions andsysModule parts overlap
Combinedsubprocess Flexible call to external command line tools Complex implementation, potentially low performance
use.collect_env Provides comprehensive environmental information, suitable for debugging The output format is complex and requires further processing

This is the end of this article about several summary methods for viewing PyTorch, cuda and Python versions. For more information about PyTorch, cuda and Python versions, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!