-
Notifications
You must be signed in to change notification settings - Fork 11
Probe matlab folders at runtime #56
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: R2025a
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import functools | ||
import os | ||
import platform | ||
import shutil | ||
from typing import Optional, NamedTuple | ||
|
||
|
||
def _get_platform_arch() -> str: | ||
system_name = platform.system() | ||
|
||
if system_name == 'Windows': | ||
return 'win64' | ||
if system_name == 'Linux': | ||
return 'glnxa64' | ||
if system_name == 'Darwin': | ||
if platform.mac_ver()[-1] == 'arm64': | ||
return 'maca64' | ||
return 'maci64' | ||
|
||
raise RuntimeError(f"{system_name} is not a supported platform.") | ||
|
||
|
||
def _get_matlab_root() -> Optional[str]: | ||
"""Probe matlab root directory""" | ||
matlab_command = shutil.which('matlab') | ||
if not matlab_command: | ||
return None | ||
matlab_bin_dir = os.path.dirname(matlab_command) | ||
matlab_root = os.path.normpath(os.path.join(matlab_bin_dir, os.pardir)) | ||
return matlab_root | ||
|
||
|
||
class MatlabPathInfo(NamedTuple): | ||
arch: str | ||
bin_folder: str | ||
engine_folder: str | ||
extern_bin: str | ||
|
||
|
||
@functools.cache | ||
def get_path_info() -> MatlabPathInfo: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Read |
||
package_folder = os.path.dirname(os.path.realpath(__file__)) | ||
arch_file = os.path.join(package_folder, 'engine', '_arch.txt') | ||
if os.path.isfile(arch_file): | ||
with open(arch_file, 'r') as root: | ||
[arch, bin_folder, engine_folder, extern_bin] = [line.strip() for line in root.readlines() if line.strip()] | ||
return MatlabPathInfo(arch, bin_folder, engine_folder, extern_bin) | ||
|
||
matlab_root = _get_matlab_root() | ||
if matlab_root: | ||
arch = _get_platform_arch() | ||
bin_folder = os.path.join(matlab_root, 'bin', arch) | ||
engine_folder = os.path.join(matlab_root, 'extern', 'engines', 'python', 'dist', 'matlab', 'engine', arch) | ||
extern_bin = os.path.join(matlab_root, 'extern', 'bin', arch) | ||
if os.path.isdir(bin_folder) and os.path.isdir(engine_folder) and os.path.isdir(extern_bin): | ||
return MatlabPathInfo(arch, bin_folder, engine_folder, extern_bin) | ||
|
||
raise RuntimeError("The MATLAB Engine for Python install is corrupted or matlab is not available. Please try to re-install.") |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,6 +27,8 @@ | |
import threading | ||
import warnings | ||
|
||
from .. import _utils | ||
|
||
# UPDATE_IF_PYTHON_VERSION_ADDED_OR_REMOVED : search for this string in codebase | ||
# when support for a Python version must be added or removed | ||
|
||
|
@@ -50,8 +52,6 @@ | |
'is %s' % _version) | ||
|
||
|
||
_module_folder = os.path.dirname(os.path.realpath(__file__)) | ||
_arch_filename = os.path.join(_module_folder, "_arch.txt") | ||
success = False | ||
firstExceptionMessage = '' | ||
secondExceptionMessage = '' | ||
|
@@ -65,21 +65,14 @@ | |
|
||
if firstExceptionMessage: | ||
try: | ||
_arch_file = open(_arch_filename,'r') | ||
_lines = _arch_file.readlines() | ||
[_arch, _bin_dir,_engine_dir, _extern_bin_dir] = [x.rstrip() for x in _lines if x.rstrip() != ""] | ||
_arch_file.close() | ||
sys.path.insert(0,_engine_dir) | ||
sys.path.insert(0,_extern_bin_dir) | ||
|
||
_envs = {'win32': 'PATH', 'win64': 'PATH'} | ||
if _arch in _envs: | ||
if _envs[_arch] in os.environ: | ||
_env = os.environ[_envs[_arch]] | ||
os.environ[_envs[_arch]] = _bin_dir + os.pathsep + os.environ[_envs[_arch]] | ||
_path_info = _utils.get_path_info() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Reuse path info rather than re-reading the file |
||
if _path_info.arch in _envs: | ||
if _envs[_path_info.arch] in os.environ: | ||
os.environ[_envs[_path_info.arch]] = _path_info.bin_folder + os.pathsep + os.environ[_envs[_path_info.arch]] | ||
else: | ||
os.environ[_envs[_arch]] = _bin_dir | ||
os.add_dll_directory(_bin_dir) | ||
os.environ[_envs[_path_info.arch]] = _path_info.bin_folder | ||
os.add_dll_directory(_path_info.bin_folder) | ||
if _PYTHONVERSION != '3_9' or _PYTHONVERSION != '3_10': | ||
pythonengine = importlib.import_module("matlabengineforpython_abi3") | ||
else: | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use env.var. to pass options, it could be replaced with extra build arguments