mirror of
https://github.com/pyenv/pyenv-virtualenv.git
synced 2025-11-10 12:33:54 -05:00
pyenv-virtualenvs could not list conda environments & pyenv shell would only activate the base conda environment the conda detection criteria of testing the presence of `conda` or `activate` files under `$(pyenv root)/versions/$version/bin` appears to be the culprit, since newer environments no longer include these files: those files reside in the base conda environment - add detection criteria of `$(pyenv root)/versions/$version/conda-meta` - compute the real prefix to the base environment from `realpath $(realpath $(pyenv root)/versions/$version)/../..` - to allow that, enhance substitute `realpath` in `pyenv-virtualenvs` to reduce relative paths `.` & `..`, and factor that code out to a file under `libexec` for reuse - hook `which` to locate conda from the real prefix
84 lines
2.7 KiB
Bash
Executable File
84 lines
2.7 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
#
|
|
# Summary: Display real_prefix for a Python virtualenv version
|
|
# Usage: pyenv virtualenv-prefix [<virtualenv>]
|
|
#
|
|
|
|
set -e
|
|
[ -n "$PYENV_DEBUG" ] && set -x
|
|
. "${BASH_SOURCE%/*}"/../libexec/pyenv-virtualenv-realpath
|
|
|
|
if [ -z "$PYENV_ROOT" ]; then
|
|
PYENV_ROOT="${HOME}/.pyenv"
|
|
fi
|
|
|
|
if [ -n "$1" ]; then
|
|
versions=($@)
|
|
IFS=: PYENV_VERSION="${versions[*]}"
|
|
export PYENV_VERSION
|
|
else
|
|
IFS=: versions=($(pyenv-version-name))
|
|
fi
|
|
|
|
append_virtualenv_prefix() {
|
|
if [ -d "${VIRTUALENV_PREFIX_PATH}" ]; then
|
|
VIRTUALENV_PREFIX_PATHS=("${VIRTUALENV_PREFIX_PATHS[@]}" "${VIRTUALENV_PREFIX_PATH:-${PYENV_PREFIX_PATH}}")
|
|
else
|
|
echo "pyenv-virtualenv: version \`${version}' is not a virtualenv" 1>&2
|
|
exit 1
|
|
fi
|
|
}
|
|
|
|
VIRTUALENV_PREFIX_PATHS=()
|
|
for version in "${versions[@]}"; do
|
|
if [ "$version" = "system" ]; then
|
|
echo "pyenv-virtualenv: version \`${version}' is not a virtualenv" 1>&2
|
|
exit 1
|
|
fi
|
|
PYENV_PREFIX_PATH="$(pyenv-prefix "${version}")"
|
|
if [ -x "${PYENV_PREFIX_PATH}/bin/python" ]; then
|
|
if [ -f "${PYENV_PREFIX_PATH}/bin/activate" ]; then
|
|
if [ -f "${PYENV_PREFIX_PATH}/bin/conda" ]; then
|
|
# conda
|
|
VIRTUALENV_PREFIX_PATH="${PYENV_PREFIX_PATH}"
|
|
else
|
|
if [ -f "${PYENV_ROOT}/versions/${version}/pyvenv.cfg" ]; then
|
|
# venv
|
|
virtualenv_binpath="$(cut -b 1-1024 "${PYENV_ROOT}/versions/${version}/pyvenv.cfg" | sed -n '/^ *home *= */s///p' || true)"
|
|
VIRTUALENV_PREFIX_PATH="${virtualenv_binpath%/bin}"
|
|
else
|
|
# virtualenv
|
|
if [ -d "${PYENV_ROOT}/versions/${version}/Lib" ]; then
|
|
# jython
|
|
virtualenv_libpath="${PYENV_ROOT}/versions/${version}/Lib"
|
|
else
|
|
if [ -d "${PYENV_ROOT}/versions/${version}/lib-python" ]; then
|
|
# pypy
|
|
virtualenv_libpath="${PYENV_ROOT}/versions/${version}/lib-python"
|
|
else
|
|
virtualenv_libpath="${PYENV_ROOT}/versions/${version}/lib"
|
|
fi
|
|
fi
|
|
virtualenv_orig_prefix="$(find "${virtualenv_libpath}/" -maxdepth 2 -type f -and -name "orig-prefix.txt" 2>/dev/null | head -1)"
|
|
if [ -f "${virtualenv_orig_prefix}" ]; then
|
|
VIRTUALENV_PREFIX_PATH="$(cat "${virtualenv_orig_prefix}" 2>/dev/null || true)"
|
|
fi
|
|
fi
|
|
fi
|
|
append_virtualenv_prefix
|
|
elif [ -d "${PYENV_PREFIX_PATH}/conda-meta" ]; then
|
|
# conda
|
|
VIRTUALENV_PREFIX_PATH="$(realpath "${PYENV_PREFIX_PATH}"/../..)"
|
|
append_virtualenv_prefix
|
|
else
|
|
echo "pyenv-virtualenv: version \`${version}' is not a virtualenv" 1>&2
|
|
exit 1
|
|
fi
|
|
else
|
|
echo "pyenv-virtualenv: \`python' not found in version \`${version}'" 1>&2
|
|
exit 1
|
|
fi
|
|
done
|
|
|
|
IFS=: echo "${VIRTUALENV_PREFIX_PATHS[*]}"
|