Skip to content
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

fix: Path issues with pip modules #4256

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 11 additions & 15 deletions src/bentoml/_internal/service/loader.py
Expand Up @@ -63,7 +63,6 @@ def import_service(
from bentoml import Service

prev_cwd = None
sys_path_modified = False
prev_cwd = os.getcwd()
global_model_store = BentoMLContainer.model_store.get()

Expand All @@ -72,18 +71,16 @@ def recover_standalone_env_change():
os.chdir(prev_cwd)
BentoMLContainer.model_store.set(global_model_store)

try:
if working_dir is not None:
working_dir = os.path.realpath(os.path.expanduser(working_dir))
# Set cwd(current working directory) to the Bento's project directory,
# which allows user code to read files using relative path
os.chdir(working_dir)
else:
working_dir = os.getcwd()
if working_dir is not None:
working_dir = os.path.realpath(os.path.expanduser(working_dir))
# Set cwd(current working directory) to the Bento's project directory,
# which allows user code to read files using relative path
os.chdir(working_dir)
else:
working_dir = os.getcwd()

if working_dir not in sys.path:
sys.path.insert(0, working_dir)
sys_path_modified = True
try:
sys.path.insert(0, working_dir)

if model_store is not global_model_store:
BentoMLContainer.model_store.set(model_store)
Expand Down Expand Up @@ -172,9 +169,8 @@ def recover_standalone_env_change():
object.__setattr__(instance, "_import_str", f"{module_name}:{attrs_str}")
return instance
except ImportServiceError:
if sys_path_modified and working_dir:
# Undo changes to sys.path
sys.path.remove(working_dir)
# Undo changes to sys.path
sys.path.remove(working_dir)

recover_standalone_env_change()
raise
Expand Down