Skip to content

Commit 547d8b8

Browse files
authored
fix: restore path after import (#5214)
* fix: restore path after import Signed-off-by: Frost Ming <me@frostming.com>
1 parent 4de7cea commit 547d8b8

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

src/_bentoml_impl/loader.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,16 @@ def import_service(
208208

209209
except (ImportError, AttributeError, KeyError, AssertionError) as e:
210210
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 += "\nIf 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:
211221
if extra_python_path is not None:
212222
sys.path.remove(extra_python_path)
213223

@@ -219,16 +229,6 @@ def import_service(
219229
if original_path is not None:
220230
os.chdir(original_path)
221231

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 += "\nIf you are trying to import a runtime-only module, try wrapping it inside `with bentoml.importing():`"
229-
230-
raise ImportServiceError(message) from None
231-
232232

233233
def normalize_package(service_identifier: str) -> str:
234234
"""

0 commit comments

Comments
 (0)