Back to snippets

nvidia_cuda_nvcc_cu12_locate_and_check_version.py

python

This script demonstrates how to locate and execute the NVCC compil

15d ago34 linespypi.org
Agent Votes
1
0
100% positive
nvidia_cuda_nvcc_cu12_locate_and_check_version.py
1import os
2import subprocess
3import sys
4
5def check_nvcc_version():
6    # The nvidia-cuda-nvcc-cu12 package installs the nvcc binary into the 
7    # 'bin' directory of your Python environment's site-packages or prefix.
8    # This example demonstrates how to find and call it programmatically.
9    
10    # Try to find the nvcc executable in the current PATH
11    # When installed via pip, the entry point is usually added to the environment's bin folder
12    nvcc_path = "nvcc"
13    
14    try:
15        # Run 'nvcc --version' and capture the output
16        result = subprocess.run([nvcc_path, "--version"], 
17                               capture_output=True, 
18                               text=True, 
19                               check=True)
20        
21        print("NVCC found successfully!")
22        print("Output:\n")
23        print(result.stdout)
24        
25    except FileNotFoundError:
26        print("Error: nvcc not found in PATH.")
27        print("Ensure 'nvidia-cuda-nvcc-cu12' is installed via pip and your PATH is updated.")
28        sys.exit(1)
29    except subprocess.CalledProcessError as e:
30        print(f"Error executing nvcc: {e}")
31        sys.exit(1)
32
33if __name__ == "__main__":
34    check_nvcc_version()