|
5 | 5 | from pathlib import Path
|
6 | 6 | import subprocess
|
7 | 7 |
|
8 |
| -from ansys.tools.installer.constants import ANSYS_VENVS |
| 8 | +from ansys.tools.installer.constants import ( |
| 9 | + ANSYS_ENV_VAR_START, |
| 10 | + ANSYS_SUPPORTED_PYTHON_VERSIONS, |
| 11 | + ANSYS_VENVS, |
| 12 | +) |
9 | 13 |
|
10 | 14 | # only used on windows
|
11 | 15 | try:
|
@@ -100,30 +104,35 @@ def _find_installed_python_win(admin=False):
|
100 | 104 | return paths
|
101 | 105 |
|
102 | 106 |
|
103 |
| -def _find_installed_python_win(admin=False): |
104 |
| - """Check the windows registry for any installed instances of Python.""" |
105 |
| - if admin: |
106 |
| - root_key = winreg.HKEY_LOCAL_MACHINE |
107 |
| - else: |
108 |
| - root_key = winreg.HKEY_CURRENT_USER |
| 107 | +def _find_installed_ansys_win(): |
| 108 | + """Check the environment variables for Ansys installations.""" |
| 109 | + env_keys = [] |
| 110 | + for key in os.environ.keys(): |
| 111 | + if key.lower().startswith(ANSYS_ENV_VAR_START): |
| 112 | + env_keys.append(key) |
| 113 | + return env_keys |
109 | 114 |
|
110 |
| - paths = {} |
111 |
| - try: |
112 |
| - base_key = "SOFTWARE\\Python\\PythonCore" |
113 |
| - with winreg.OpenKey( |
114 |
| - root_key, |
115 |
| - base_key, |
116 |
| - access=winreg.KEY_READ, |
117 |
| - ) as reg_key: |
118 |
| - info = winreg.QueryInfoKey(reg_key) |
119 |
| - for i in range(info[0]): |
120 |
| - name = winreg.EnumKey(reg_key, i) |
121 |
| - ver, path = _get_python_info_win(f"{base_key}\\{name}", root_key) |
122 |
| - if ver is not None and path is not None: |
123 |
| - paths[path] = (ver, admin) |
124 | 115 |
|
125 |
| - except FileNotFoundError: |
126 |
| - pass |
| 116 | +def _find_installed_ansys_python_win(): |
| 117 | + """Check the Ansys installation folder for installed Python.""" |
| 118 | + installed_ansys = _find_installed_ansys_win() |
| 119 | + paths = {} |
| 120 | + for ansys_ver_env_key in installed_ansys: |
| 121 | + ansys_path = os.environ[ansys_ver_env_key] |
| 122 | + for ansys_py_ver in ANSYS_SUPPORTED_PYTHON_VERSIONS: |
| 123 | + path = os.path.join( |
| 124 | + ansys_path, |
| 125 | + f"commonfiles\\CPython\\{ansys_py_ver}\\winx64\\Release\\python", |
| 126 | + ) |
| 127 | + if os.path.exists(path): |
| 128 | + version_output = subprocess.check_output( |
| 129 | + [f"{path}\\python.exe", "--version"], text=True |
| 130 | + ).strip() |
| 131 | + try: |
| 132 | + version = version_output.split()[1] |
| 133 | + paths[path] = (version, False) |
| 134 | + except Exception: |
| 135 | + pass |
127 | 136 |
|
128 | 137 | return paths
|
129 | 138 |
|
@@ -204,6 +213,7 @@ def find_all_python():
|
204 | 213 | if os.name == "nt":
|
205 | 214 | paths = _find_installed_python_win(True)
|
206 | 215 | paths.update(_find_installed_python_win(False))
|
| 216 | + paths.update(_find_installed_ansys_python_win()) |
207 | 217 | else:
|
208 | 218 | paths = _find_installed_python_linux()
|
209 | 219 |
|
|
0 commit comments