Skip to content

Commit 1c77405

Browse files
added support for ansys python (#96)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent ba89bac commit 1c77405

File tree

2 files changed

+40
-23
lines changed

2 files changed

+40
-23
lines changed

src/ansys/tools/installer/constants.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
LOG = logging.getLogger(__name__)
1010
LOG.setLevel("DEBUG")
11+
ANSYS_ENV_VAR_START = "awp_root"
12+
ANSYS_SUPPORTED_PYTHON_VERSIONS = ["3_7", "3_10"]
1113

1214

1315
ABOUT_TEXT = f"""<h2>Ansys Python Installer {__version__}</h2>

src/ansys/tools/installer/find_python.py

Lines changed: 38 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@
55
from pathlib import Path
66
import subprocess
77

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+
)
913

1014
# only used on windows
1115
try:
@@ -100,30 +104,40 @@ def _find_installed_python_win(admin=False):
100104
return paths
101105

102106

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
109114

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)
124115

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)
127141

128142
return paths
129143

@@ -204,6 +218,7 @@ def find_all_python():
204218
if os.name == "nt":
205219
paths = _find_installed_python_win(True)
206220
paths.update(_find_installed_python_win(False))
221+
paths.update(_find_installed_ansys_python_win())
207222
else:
208223
paths = _find_installed_python_linux()
209224

0 commit comments

Comments
 (0)