Skip to content

Commit c13954a

Browse files
authored
Merge pull request #1228 from LourensVeen/issue_1192_amuse_lab_installation_message
Have amuse.lab raise original ImportError if code is missing
2 parents 151578a + a8f0ab3 commit c13954a

1 file changed

Lines changed: 23 additions & 41 deletions

File tree

src/amuse/lab.py

Lines changed: 23 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -48,42 +48,33 @@
4848
new_spherical_particle_distribution
4949
)
5050

51+
52+
def _placeholder(e):
53+
class _placeholder(object):
54+
def __init__(self, *arg, **kwargs):
55+
raise e
56+
return _placeholder
57+
58+
5159
try:
5260
from amuse.ext.halogen_model import new_halogen_model
53-
except ImportError:
54-
def new_halogen_model():
55-
print(
56-
"Error - Halogen not installed. Install it with 'pip install "
57-
"amuse-halogen'."
58-
)
59-
return -1
61+
except ImportError as e:
62+
locals()["new_halogen_model"] = _placeholder(e)
63+
64+
6065
try:
6166
from amuse.ext.galactics_model import new_galactics_model
62-
except ImportError:
63-
def new_galactics_model():
64-
print(
65-
"Error - Galactics not installed. Install it with 'pip install "
66-
"amuse-galactics'."
67-
)
68-
return -1
67+
except ImportError as e:
68+
locals()["new_galactics_model"] = _placeholder(e)
69+
70+
6971
try:
7072
from amuse.ext.star_to_sph import (
7173
convert_stellar_model_to_SPH, pickle_stellar_model,
7274
)
73-
except ImportError:
74-
def convert_stellar_model_to_SPH():
75-
print(
76-
"Error - Gadget2 not installed. Install it with 'pip install "
77-
"amuse-gadget2'."
78-
)
79-
return -1
80-
81-
def pickle_stellar_model():
82-
print(
83-
"Error - Gadget2 not installed. Install it with 'pip install "
84-
"amuse-gadget2'."
85-
)
86-
return -1
75+
except ImportError as e:
76+
locals()["convert_stellar_model_to_SPH"] = _placeholder(e)
77+
locals()["pickle_stellar_model"] = _placeholder(e)
8778

8879

8980
_community_codes = [
@@ -127,15 +118,6 @@ def pickle_stellar_model():
127118
]
128119

129120

130-
def _placeholder(name):
131-
class _placeholder(object):
132-
def __init__(self, *arg, **kwargs):
133-
raise Exception(
134-
"failed import, code {0} not available, maybe it needs to be "
135-
"(pip) installed?".format(name))
136-
return _placeholder
137-
138-
139121
for _name in _community_codes:
140122
_interfacename = _name+"Interface"
141123
# future fix: _interfacename = _name.title()+"Interface"
@@ -149,10 +131,10 @@ def __init__(self, *arg, **kwargs):
149131
locals()[_name] = getattr(_interface, _name)
150132
locals()[_interfacename] = getattr(_interface, _interfacename)
151133
locals()[_standardisedname] = getattr(_interface, _standardisedname)
152-
except ImportError:
153-
locals()[_name] = _placeholder(_packagename)
154-
locals()[_interfacename] = _placeholder(_packagename)
155-
locals()[_standardisedname] = _placeholder(_packagename)
134+
except ImportError as e:
135+
locals()[_name] = _placeholder(e)
136+
locals()[_interfacename] = _placeholder(e)
137+
locals()[_standardisedname] = _placeholder(e)
156138

157139

158140
def vector(value=[], unit=None):

0 commit comments

Comments
 (0)