mirror of
https://github.com/pyenv/pyenv-virtualenv.git
synced 2026-09-10 07:16:18 -04:00
This means that pyenv-virtualenv will not allow manual deactivation of virtualenv if pyenv-virtualenv-init is enabled. This must be acceptable since the activation of virtualenv is just setting of environment variables in current implementation.
216 lines
4.8 KiB
Bash
Executable file
216 lines
4.8 KiB
Bash
Executable file
#!/usr/bin/env bash
|
|
#
|
|
# Summary: Activate virtual environment
|
|
#
|
|
# Usage: pyenv activate <virtualenv>
|
|
# pyenv activate --unset
|
|
#
|
|
# Activate a Python virtualenv environment in current shell.
|
|
# This acts almost as same as `pyenv shell`, but this invokes the `activate`
|
|
# script in your shell.
|
|
#
|
|
# <virtualenv> should be a string matching a Python version known to pyenv.
|
|
|
|
set -e
|
|
[ -n "$PYENV_DEBUG" ] && set -x
|
|
|
|
if [ -z "${PYENV_ROOT}" ]; then
|
|
PYENV_ROOT="$(pyenv-root)"
|
|
fi
|
|
|
|
resolve_link() {
|
|
$(type -p greadlink readlink | head -1) "$1"
|
|
}
|
|
|
|
unset FORCE
|
|
unset QUIET
|
|
unset VERBOSE
|
|
|
|
while [ $# -gt 0 ]; do
|
|
case "$1" in
|
|
"--complete" )
|
|
# Provide pyenv completions
|
|
echo --unset
|
|
exec pyenv-virtualenvs --bare
|
|
;;
|
|
"-f" | "--force" )
|
|
FORCE=1
|
|
;;
|
|
"-q" | "--quiet" )
|
|
QUIET=1
|
|
;;
|
|
"--unset" )
|
|
exec pyenv-sh-deactivate
|
|
;;
|
|
"-v" | "--verbose" )
|
|
VERBOSE=1
|
|
;;
|
|
* )
|
|
break
|
|
;;
|
|
esac
|
|
shift 1
|
|
done
|
|
|
|
no_shell=
|
|
versions=("$@")
|
|
if [ -z "${versions}" ]; then
|
|
no_shell=1
|
|
OLDIFS="$IFS"
|
|
IFS=: versions=($(pyenv-version-name))
|
|
IFS="$OLDIFS"
|
|
fi
|
|
|
|
if [ -z "${PYENV_VIRTUALENV_INIT}" ]; then
|
|
# Backward compatibility issue
|
|
# https://github.com/yyuu/pyenv-virtualenv/issues/26
|
|
no_shell=
|
|
fi
|
|
|
|
venv="${versions}"
|
|
|
|
# exit as success if some virtualenv outside from pyenv is already activated
|
|
if [ -n "${VIRTUAL_ENV}" ] && [[ "${VIRTUAL_ENV}" == "${VIRTUAL_ENV#${PYENV_ROOT}/versions/}" ]]; then
|
|
if [ -z "${FORCE}" ]; then
|
|
if [ -z "${QUIET}" ]; then
|
|
echo "pyenv-virtualenv: virtualenv \`${VIRTUAL_ENV}' is already activated" 1>&2
|
|
fi
|
|
echo "true"
|
|
exit 0
|
|
fi
|
|
fi
|
|
|
|
if ! pyenv-virtualenv-prefix "${venv}" 1>/dev/null 2>&1; then
|
|
if [ -z "${QUIET}" ]; then
|
|
echo "pyenv-virtualenv: version \`${venv}' is not a virtualenv" 1>&2
|
|
fi
|
|
echo "false"
|
|
exit 1
|
|
fi
|
|
|
|
# exit as error if there are multiple virtualenvs
|
|
# https://github.com/yyuu/pyenv-virtualenv/issues/105
|
|
for version in "${versions[@]}"; do
|
|
if [[ "${version}" != "${venv}" ]]; then
|
|
if pyenv-virtualenv-prefix "${version}" 1>/dev/null 2>&1; then
|
|
if [ -z "${QUIET}" ]; then
|
|
echo "pyenv-virtualenv: cannot activate multiple versions at once: ${versions[@]}" 1>&2
|
|
fi
|
|
echo "false"
|
|
exit 1
|
|
fi
|
|
fi
|
|
done
|
|
|
|
shell="${PYENV_SHELL:-${SHELL##*/}}"
|
|
prefix="$(pyenv-prefix "${venv}")"
|
|
|
|
if [ -L "${prefix}" ]; then
|
|
prefix="$(resolve_link "${prefix}" 2>/dev/null)"
|
|
fi
|
|
|
|
# exit as success if the virtualenv is already activated
|
|
if [[ "${VIRTUAL_ENV}" == "${prefix}" ]]; then
|
|
if [ -z "${FORCE}" ]; then
|
|
if [ -z "${QUIET}" ]; then
|
|
echo "pyenv-virtualenv: version \`${venv}' is already activated" 1>&2
|
|
fi
|
|
echo "true"
|
|
exit 0
|
|
fi
|
|
fi
|
|
|
|
# Display setup instruction, if pyenv-virtualenv has not been initialized.
|
|
# if 'pyenv virtualenv-init -' is not found in "$profile"
|
|
if [ -z "$PYENV_VIRTUALENV_INIT" ]; then
|
|
pyenv-virtualenv-init >&2 || true
|
|
fi
|
|
|
|
pyenv-sh-deactivate --quiet ${VERBOSE+--verbose} || true
|
|
|
|
echo "pyenv-virtualenv: activate ${venv}" 1>&2
|
|
|
|
if [ -z "$no_shell" ]; then
|
|
# shell version set in pyenv-sh-activate should be unset
|
|
# https://github.com/yyuu/pyenv-virtualenv/issues/61
|
|
OLDIFS="$IFS"
|
|
IFS=:
|
|
case "$shell" in
|
|
fish )
|
|
cat <<EOS
|
|
setenv PYENV_VERSION "${versions[*]}";
|
|
setenv PYENV_ACTIVATE_SHELL 1;
|
|
EOS
|
|
;;
|
|
* )
|
|
cat <<EOS
|
|
export PYENV_VERSION="${versions[*]}";
|
|
export PYENV_ACTIVATE_SHELL=1;
|
|
EOS
|
|
;;
|
|
esac
|
|
IFS="$OLDIFS"
|
|
fi
|
|
|
|
# virtualenv/pyvenv
|
|
case "${shell}" in
|
|
fish )
|
|
echo "setenv VIRTUAL_ENV \"${prefix}\";"
|
|
;;
|
|
* )
|
|
echo "export VIRTUAL_ENV=\"${prefix}\";"
|
|
;;
|
|
esac
|
|
|
|
# anaconda/miniconda
|
|
if [ -x "${prefix}/bin/conda" ]; then
|
|
if [[ "${venv}" != "${venv%/envs/*}" ]]; then
|
|
CONDA_DEFAULT_ENV="${venv##*/envs/}"
|
|
else
|
|
CONDA_DEFAULT_ENV="root"
|
|
fi
|
|
case "${shell}" in
|
|
fish )
|
|
echo "setenv CONDA_DEFAULT_ENV \"${CONDA_DEFAULT_ENV}\";"
|
|
;;
|
|
* )
|
|
echo "export CONDA_DEFAULT_ENV=\"${CONDA_DEFAULT_ENV}\";"
|
|
;;
|
|
esac
|
|
fi
|
|
|
|
if [ -n "${PYTHONHOME}" ]; then
|
|
case "${shell}" in
|
|
fish )
|
|
cat <<EOS
|
|
setenv _OLD_VIRTUAL_PYTHONHOME "${PYTHONHOME}";
|
|
set -e PYTHONHOME;
|
|
EOS
|
|
;;
|
|
* )
|
|
cat <<EOS
|
|
export _OLD_VIRTUAL_PYTHONHOME="${PYTHONHOME}";
|
|
unset PYTHONHOME;
|
|
EOS
|
|
;;
|
|
esac
|
|
fi
|
|
|
|
PYENV_VIRTUALENV_DISABLE_PROMPT="${PYENV_VIRTUALENV_DISABLE_PROMPT:-${PYENV_VIRTUAL_ENV_DISABLE_PROMPT}}"
|
|
PYENV_VIRTUALENV_DISABLE_PROMPT="${PYENV_VIRTUALENV_DISABLE_PROMPT:-${VIRTUAL_ENV_DISABLE_PROMPT}}"
|
|
|
|
if [ -z "${PYENV_VIRTUALENV_DISABLE_PROMPT}" ]; then
|
|
case "${shell}" in
|
|
fish )
|
|
echo "pyenv-virtualenv: prompt changing not work for fish." 1>&2
|
|
;;
|
|
* )
|
|
echo "pyenv-virtualenv: prompt changing will be removed from future release. configure \`export PYENV_VIRTUALENV_DISABLE_PROMPT=1' to simulate the behavior." 1>&2
|
|
cat <<EOS
|
|
export _OLD_VIRTUAL_PS1="\${PS1}";
|
|
export PS1="(${venv}) \${PS1}";
|
|
EOS
|
|
;;
|
|
esac
|
|
fi
|