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

PR: Add condabin directory, relative to sys.executable and provided pyexec, to paths searched for conda executable #21843

Merged
merged 3 commits into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
23 changes: 18 additions & 5 deletions spyder/utils/conda.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,17 @@ def get_conda_root_prefix(pyexec=None, quote=False):
return root_prefix


def get_conda_activation_script(quote=False):
def get_conda_activation_script(pyexec=None, quote=False):
"""
Return full path to conda activation script.

`pyexec` is optional python executable, the relative location from which to
attempt to find the activation script.

If `quote` is True, then quotes are added if spaces are found in the path.
"""
# Use micromamba bundled with Spyder installers or find conda exe
exe = get_spyder_umamba_path() or find_conda()
exe = get_spyder_umamba_path() or find_conda(pyexec)

if osp.basename(exe).startswith('micromamba'):
# For Micromamba, use the executable
Expand Down Expand Up @@ -130,14 +133,24 @@ def get_conda_env_path(pyexec, quote=False):
return conda_env


def find_conda():
"""Find conda executable."""
def find_conda(pyexec=None):
"""
Find conda executable.

`pyexec` is a python executable, the relative location from which to
attempt to locate a conda executable.
"""
# First try the environment variables
conda = os.environ.get('CONDA_EXE') or os.environ.get('MAMBA_EXE')
if conda is None:
# Try searching for the executable
conda_exec = 'conda.bat' if WINDOWS else 'conda'
mrclary marked this conversation as resolved.
Show resolved Hide resolved
conda = find_program(conda_exec)
extra_paths = [
osp.join(get_conda_root_prefix(_pyexec), 'condabin')
for _pyexec in [sys.executable, pyexec]
]
conda = find_program(conda_exec, extra_paths)

return conda


Expand Down
12 changes: 6 additions & 6 deletions spyder/utils/programs.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,15 @@ def get_temp_dir(suffix=None):
return tempdir


def is_program_installed(basename):
def is_program_installed(basename, extra_paths=[]):
"""
Return program absolute path if installed in PATH.
Otherwise, return None.

Also searches specific platform dependent paths that are not already in
PATH. This permits general use without assuming user profiles are
sourced (e.g. .bash_Profile), such as when login shells are not used to
launch Spyder.
launch Spyder. Additionally, extra_paths are searched.

On macOS systems, a .app is considered installed if it exists.
"""
Expand Down Expand Up @@ -107,15 +107,15 @@ def is_program_installed(basename):
'Miniconda3', 'Anaconda3', 'Miniconda', 'Anaconda']

conda = [osp.join(*p, 'condabin') for p in itertools.product(a, b)]
req_paths.extend(pyenv + conda)
req_paths.extend(pyenv + conda + extra_paths)

for path in os.environ['PATH'].split(os.pathsep) + req_paths:
for path in set(os.environ['PATH'].split(os.pathsep) + req_paths):
abspath = osp.join(path, basename)
if osp.isfile(abspath):
return abspath


def find_program(basename):
def find_program(basename, extra_paths=[]):
"""
Find program in PATH and return absolute path

Expand All @@ -129,7 +129,7 @@ def find_program(basename):
if not basename.endswith(extensions):
names = [basename + ext for ext in extensions] + [basename]
for name in names:
path = is_program_installed(name)
path = is_program_installed(name, extra_paths)
if path:
return path

Expand Down
9 changes: 9 additions & 0 deletions spyder/utils/tests/test_conda.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,17 @@ def test_get_conda_root_prefix():

@pytest.mark.skipif(not running_in_ci(), reason="Only meant for CIs")
def test_find_conda():
ccordoba12 marked this conversation as resolved.
Show resolved Hide resolved
# Temporarily remove CONDA_EXE and MAMBA_EXE, if present
conda_exe = os.environ.pop('CONDA_EXE', None)
mamba_exe = os.environ.pop('MAMBA_EXE', None)
ccordoba12 marked this conversation as resolved.
Show resolved Hide resolved

assert find_conda()

if conda_exe is not None:
os.environ['CONDA_EXE'] = conda_exe
if mamba_exe is not None:
os.environ['MAMBA_EXE'] = mamba_exe


@pytest.mark.skipif(not running_in_ci(), reason="Only meant for CIs")
def test_get_list_conda_envs():
Expand Down
Loading