|
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,40 @@ 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 installation.""" |
| 109 | + env_keys = list() |
| 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 | + ansys_python_path_items = [ |
| 121 | + "commonfiles", |
| 122 | + "CPython", |
| 123 | + "<platform>", |
| 124 | + "winx64", |
| 125 | + "Release", |
| 126 | + "python", |
| 127 | + "python.exe", |
| 128 | + ] |
| 129 | + for ansys_ver_env_key in installed_ansys: |
| 130 | + ansys_path = os.environ[ansys_ver_env_key] |
| 131 | + for supp_py_ver in ANSYS_SUPPORTED_PYTHON_VERSIONS: |
| 132 | + ansys_python_path_items[2] = supp_py_ver |
| 133 | + path = os.path.join(ansys_path, *ansys_python_path_items) |
| 134 | + if os.path.exists(path): |
| 135 | + version_output = subprocess.check_output( |
| 136 | + [path, "--version"], text=True |
| 137 | + ).strip() |
| 138 | + version = version_output.split()[1] |
| 139 | + if version is not None and path is not None: |
| 140 | + paths[path] = (version, False) |
127 | 141 |
|
128 | 142 | return paths |
129 | 143 |
|
@@ -204,6 +218,7 @@ def find_all_python(): |
204 | 218 | if os.name == "nt": |
205 | 219 | paths = _find_installed_python_win(True) |
206 | 220 | paths.update(_find_installed_python_win(False)) |
| 221 | + paths.update(_find_installed_ansys_python_win()) |
207 | 222 | else: |
208 | 223 | paths = _find_installed_python_linux() |
209 | 224 |
|
|
0 commit comments