@@ -208,6 +208,16 @@ def import_service(
208
208
209
209
except (ImportError , AttributeError , KeyError , AssertionError ) as e :
210
210
sys_path = sys .path .copy ()
211
+ message = f'Failed to import service "{ service_identifier } ": { e } , sys.path: { sys_path } , cwd: { pathlib .Path .cwd ()} '
212
+ if (
213
+ isinstance (e , ImportError )
214
+ and server_context .worker_index is None
215
+ and not (e .name or "" ).startswith (("bentoml" , "_bentoml_" ))
216
+ ):
217
+ message += "\n If you are trying to import a runtime-only module, try wrapping it inside `with bentoml.importing():`"
218
+
219
+ raise ImportServiceError (message ) from None
220
+ finally :
211
221
if extra_python_path is not None :
212
222
sys .path .remove (extra_python_path )
213
223
@@ -219,16 +229,6 @@ def import_service(
219
229
if original_path is not None :
220
230
os .chdir (original_path )
221
231
222
- message = f'Failed to import service "{ service_identifier } ": { e } , sys.path: { sys_path } , cwd: { pathlib .Path .cwd ()} '
223
- if (
224
- isinstance (e , ImportError )
225
- and server_context .worker_index is None
226
- and not (e .name or "" ).startswith (("bentoml" , "_bentoml_" ))
227
- ):
228
- message += "\n If you are trying to import a runtime-only module, try wrapping it inside `with bentoml.importing():`"
229
-
230
- raise ImportServiceError (message ) from None
231
-
232
232
233
233
def normalize_package (service_identifier : str ) -> str :
234
234
"""
0 commit comments