forked from cropsinsilico/yggdrasil
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall_matlab_engine.py
More file actions
65 lines (58 loc) · 1.94 KB
/
install_matlab_engine.py
File metadata and controls
65 lines (58 loc) · 1.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import os
import sys
import subprocess
PY_MAJOR_VERSION = sys.version_info[0]
def install_matlab(as_user=False):
r"""Attempt to install the MATLAB engine API for Python.
Arguments:
as_user (bool, optional): If True, the install will be called with
--user for a local install. Defaults to False.
Returns:
bool: True if install succeded, False otherwise.
"""
# Check to see if its already installed
try:
import matlab.engine
return True
except ImportError:
pass
# Get location of matlab root
mtl_id = '=MATLABROOT='
cmd = "fprintf('" + mtl_id + "%s" + mtl_id + "', matlabroot); exit();"
mtl_cmd = ['matlab', '-nodisplay', '-nosplash', '-nodesktop', '-nojvm',
'-r', '%s' % cmd]
try:
mtl_proc = subprocess.check_output(mtl_cmd)
if PY_MAJOR_VERSION == 3:
mtl_proc = mtl_proc.decode("utf-8")
except BaseException:
return False
if mtl_id not in mtl_proc:
return False
mtl_root = mtl_proc.split(mtl_id)[-2]
# Install engine API
mtl_setup = os.path.join(mtl_root, 'extern', 'engines', 'python')
if not os.path.isdir(mtl_setup):
return False
blddir = os.path.join(os.path.expanduser('~'), 'matlab_python_api')
if not os.path.isdir(blddir):
os.mkdir(blddir)
cmd = [sys.executable, 'setup.py',
'build', '--build-base=%s' % blddir,
'install']
if '--user' in sys.argv:
cmd.append('--user')
try:
result = subprocess.check_output(cmd, cwd=mtl_setup)
if PY_MAJOR_VERSION == 3:
result = result.decode("utf-8")
print(result)
except subprocess.CalledProcessError:
return False
return True
if __name__ == "__main__":
out = install_matlab(as_user=('--user' in sys.argv))
if out:
print("MATLAB engine installed.")
else:
raise Exception("Failed to install MATLAB engine.")