From 3bd91ffeee4f273b08f86567be75c33d3ae8b135 Mon Sep 17 00:00:00 2001 From: architkulkarni Date: Thu, 7 Jan 2021 11:59:36 -0800 Subject: [PATCH 1/4] add support for driver not in conda env or base --- .../ray/serve/examples/doc/imported_backend.py | 1 + python/ray/serve/tests/test_util.py | 3 ++- python/ray/serve/utils.py | 17 +++++++++++++---- 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/python/ray/serve/examples/doc/imported_backend.py b/python/ray/serve/examples/doc/imported_backend.py index ec864c2115390..d80d73b4a72c7 100644 --- a/python/ray/serve/examples/doc/imported_backend.py +++ b/python/ray/serve/examples/doc/imported_backend.py @@ -5,6 +5,7 @@ client = serve.start() +# Include your class as input to the ImportedBackend constructor. backend_class = ImportedBackend("ray.serve.utils.MockImportedBackend") client.create_backend("imported", backend_class, "input_arg") client.create_endpoint("imported", backend="imported", route="/imported") diff --git a/python/ray/serve/tests/test_util.py b/python/ray/serve/tests/test_util.py index 9893bc4cee3ec..cc95e41b8e9f2 100644 --- a/python/ray/serve/tests/test_util.py +++ b/python/ray/serve/tests/test_util.py @@ -10,6 +10,7 @@ from ray.serve.utils import (ServeEncoder, chain_future, unpack_future, try_schedule_resources_on_nodes, get_conda_env_dir, import_class) +from ray.serve.exceptions import RayServeException def test_bytes_encoder(): @@ -116,7 +117,7 @@ def test_get_conda_env_dir(tmp_path): d = tmp_path / "tf1" d.mkdir() os.environ["CONDA_PREFIX"] = str(d) - with pytest.raises(ValueError): + with pytest.raises(RayServeException): # env does not exist env_dir = get_conda_env_dir("tf2") tf2_dir = tmp_path / "tf2" diff --git a/python/ray/serve/utils.py b/python/ray/serve/utils.py index 7244ca4db396d..49516fd342649 100644 --- a/python/ray/serve/utils.py +++ b/python/ray/serve/utils.py @@ -180,13 +180,22 @@ def format_actor_name(actor_name, controller_name=None, *modifiers): def get_conda_env_dir(env_name): """Given a environment name like `tf1`, find and validate the - corresponding conda directory. + corresponding conda directory. Untested on Windows. """ conda_prefix = os.environ.get("CONDA_PREFIX") if conda_prefix is None: - raise ValueError( - "Serve cannot find environment variables installed by conda. " + - "Are you sure you are in a conda env?") + # The caller is neither in a conda env or in (base). This is rare + # because by default, new terminals start in (base), but we can still + # support this case. + conda_exe = os.environ.get("CONDA_EXE") + if conda_exe is None: + raise RayServeException( + "Ray Serve cannot find environment variables set by conda. " + "Please verify conda is installed.") + # Example: CONDA_EXE=$HOME/anaconda3/bin/python + # Strip out the /bin/python by going up two parent directories. + conda_prefix = os.path.abspath( + os.path.join(conda_exe, f"{os.pardir}/{os.pardir}")) # There are two cases: # 1. We are in conda base env: CONDA_DEFAULT_ENV=base and From 4b6f9ae3e80e075dba59f462bf4e0a0b77c5cccf Mon Sep 17 00:00:00 2001 From: architkulkarni Date: Thu, 7 Jan 2021 15:16:33 -0800 Subject: [PATCH 2/4] fix test --- python/ray/serve/tests/test_util.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/python/ray/serve/tests/test_util.py b/python/ray/serve/tests/test_util.py index cc95e41b8e9f2..9893bc4cee3ec 100644 --- a/python/ray/serve/tests/test_util.py +++ b/python/ray/serve/tests/test_util.py @@ -10,7 +10,6 @@ from ray.serve.utils import (ServeEncoder, chain_future, unpack_future, try_schedule_resources_on_nodes, get_conda_env_dir, import_class) -from ray.serve.exceptions import RayServeException def test_bytes_encoder(): @@ -117,7 +116,7 @@ def test_get_conda_env_dir(tmp_path): d = tmp_path / "tf1" d.mkdir() os.environ["CONDA_PREFIX"] = str(d) - with pytest.raises(RayServeException): + with pytest.raises(ValueError): # env does not exist env_dir = get_conda_env_dir("tf2") tf2_dir = tmp_path / "tf2" From 40942c8db3cf40cabe295d778b0e99260c632f3c Mon Sep 17 00:00:00 2001 From: architkulkarni Date: Thu, 7 Jan 2021 15:16:44 -0800 Subject: [PATCH 3/4] use pathlib for readability --- python/ray/serve/utils.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/python/ray/serve/utils.py b/python/ray/serve/utils.py index 49516fd342649..6d15e53fa56f8 100644 --- a/python/ray/serve/utils.py +++ b/python/ray/serve/utils.py @@ -11,6 +11,7 @@ import os from ray.serve.exceptions import RayServeException from collections import UserDict +from pathlib import Path import starlette.requests import requests @@ -194,8 +195,7 @@ def get_conda_env_dir(env_name): "Please verify conda is installed.") # Example: CONDA_EXE=$HOME/anaconda3/bin/python # Strip out the /bin/python by going up two parent directories. - conda_prefix = os.path.abspath( - os.path.join(conda_exe, f"{os.pardir}/{os.pardir}")) + conda_prefix = str(Path(conda_exe).parent.parent) # There are two cases: # 1. We are in conda base env: CONDA_DEFAULT_ENV=base and From c2210b0fa48a4ce757d607a95ed793108d2e8e45 Mon Sep 17 00:00:00 2001 From: architkulkarni Date: Thu, 7 Jan 2021 15:16:52 -0800 Subject: [PATCH 4/4] update docs --- doc/source/serve/advanced.rst | 5 ----- 1 file changed, 5 deletions(-) diff --git a/doc/source/serve/advanced.rst b/doc/source/serve/advanced.rst index dc386ee119add..4ec71103e6cd3 100644 --- a/doc/source/serve/advanced.rst +++ b/doc/source/serve/advanced.rst @@ -386,11 +386,6 @@ as shown below. .. literalinclude:: ../../../python/ray/serve/examples/doc/conda_env.py -.. warning:: - The script must be run in an activated conda environment (not required to be - ``ray-tf1`` or ``ray-tf2``). We hope to remove this restriction in the - future. - .. note:: If the argument ``env`` is omitted, backends will be started in the same conda environment as the caller of