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

Change conda env validity checking to activate and run Python #48552

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 5 additions & 8 deletions python/ray/_private/runtime_env/conda.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@
create_conda_env_if_needed,
delete_conda_env,
get_conda_activate_commands,
get_conda_info_json,
get_conda_envs,
check_if_conda_environment_valid,
)
from ray._private.runtime_env.context import RuntimeEnvContext
from ray._private.runtime_env.packaging import Protocol, parse_uri
Expand Down Expand Up @@ -343,15 +342,13 @@ def _create():
if result in self._validated_named_conda_env:
return 0

conda_info = get_conda_info_json()
envs = get_conda_envs(conda_info)

# We accept `result` as a conda name or full path.
if not any(result == env[0] or result == env[1] for env in envs):
if not check_if_conda_environment_valid(result):
raise ValueError(
f"The given conda environment '{result}' "
f"from the runtime env {runtime_env} doesn't "
"exist from the output of `conda info --json`. "
f"from the runtime env {runtime_env} can't "
f"be activated with conda activate {result} 1>&2 "
"&& python --version"
"You can only specify an env that already exists. "
f"Please make sure to create an env {result} "
)
Expand Down
22 changes: 19 additions & 3 deletions python/ray/_private/runtime_env/conda_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import subprocess
import hashlib
import json
from typing import Optional, List, Union, Tuple
from typing import Optional, List, Tuple

"""Utilities for conda. Adapted from https://github.com/mlflow/mlflow."""

Expand Down Expand Up @@ -213,8 +213,11 @@ class ShellCommandException(Exception):


def exec_cmd(
cmd: List[str], throw_on_error: bool = True, logger: Optional[logging.Logger] = None
) -> Union[int, Tuple[int, str, str]]:
cmd: List[str],
throw_on_error: bool = True,
shell: bool = False,
logger: Optional[logging.Logger] = None,
) -> Tuple[int, str, str]:
"""
Runs a command as a child process.

Expand All @@ -234,6 +237,7 @@ def exec_cmd(
stdin=subprocess.PIPE,
stderr=subprocess.PIPE,
universal_newlines=True,
shell=shell,
)
(stdout, stderr) = child.communicate()
exit_code = child.wait()
Expand Down Expand Up @@ -276,3 +280,15 @@ def exec_cmd_stream_to_logger(

exit_code = child.wait()
return exit_code, "\n".join(last_n_lines)


def check_if_conda_environment_valid(conda_env_name: str) -> bool:
"""
Check if the given conda environment exists and is valid.
We try to `conda activate` it and check that python is runnable
"""
activate_cmd = get_conda_activate_commands(conda_env_name) + ["python", "--version"]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to deactivate it afterwards?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't believe so, because conda activate sets environment variables and other things that aren't global state, which will be reset when the subprocess exits.

However, I did recently do some thinking about it, and I think conda activate does a lot of shell-specific things, so I've changed it to execute in a shell.

exit_code, _, _ = exec_cmd(
[" ".join(activate_cmd)], throw_on_error=False, shell=True
)
return exit_code == 0
4 changes: 1 addition & 3 deletions python/ray/tests/test_runtime_env_conda_and_pip.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,9 +225,7 @@ def f():
for ref in refs:
with pytest.raises(ray.exceptions.RuntimeEnvSetupError) as exc_info:
ray.get(ref)
assert "doesn't exist from the output of `conda info --json`" in str(
exc_info.value
) # noqa
assert "can't be activated with" in str(exc_info.value) # noqa


def test_get_requirements_file():
Expand Down
Loading