mirror of
https://github.com/pyenv/pyenv-virtualenv.git
synced 2025-11-12 05:23:53 -05:00
89 lines
1.9 KiB
Bash
Executable File
89 lines
1.9 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
|
|
|
|
# Provide pyenv completions
|
|
if [ "$1" = "--complete" ]; then
|
|
echo --unset
|
|
exec pyenv-virtualenvs --bare
|
|
fi
|
|
|
|
if [ "$1" = "--unset" ]; then
|
|
echo "pyenv deactivate"
|
|
exit
|
|
fi
|
|
|
|
versions=("$@")
|
|
if [ -z "$versions" ]; then
|
|
no_shell=1
|
|
OLDIFS="$IFS"
|
|
IFS=: versions=($(pyenv-version-name))
|
|
IFS="$OLDIFS"
|
|
fi
|
|
|
|
if [ "${#versions[@]}" -gt 1 ]; then
|
|
echo "pyenv-virtualenv: cannot activate multiple versions at once: ${versions[@]}" 1>&2
|
|
exit 1
|
|
fi
|
|
|
|
pyenv-virtualenv-prefix "${versions}" 1>/dev/null
|
|
|
|
shell="$(basename "${PYENV_SHELL:-$SHELL}")"
|
|
|
|
case "$shell" in
|
|
bash )
|
|
profile="$HOME/.bash_profile"
|
|
;;
|
|
zsh )
|
|
profile="$HOME/.zshrc"
|
|
;;
|
|
ksh )
|
|
profile="$HOME/.profile"
|
|
;;
|
|
fish )
|
|
profile="$HOME/.config/fish/config.fish"
|
|
;;
|
|
* )
|
|
profile="$HOME/.profile"
|
|
;;
|
|
esac
|
|
|
|
if [ -f "$profile" ]; then
|
|
if grep -q 'pyenv init -' "$profile" && ! grep -q 'pyenv virtualenv-init' "$profile"; then
|
|
{ echo "# Load pyenv-virtualenv automatically by adding"
|
|
echo "# the following to ${profile}:"
|
|
echo
|
|
case "$shell" in
|
|
fish )
|
|
echo 'status --is-interactive; and . (pyenv virtualenv-init -|psub)'
|
|
;;
|
|
* )
|
|
echo 'eval "$(pyenv virtualenv-init -)"'
|
|
;;
|
|
esac
|
|
echo
|
|
} >&2
|
|
fi
|
|
fi
|
|
|
|
if [ -z "$no_shell" ]; then
|
|
echo "pyenv shell \"${versions}\";"
|
|
fi
|
|
|
|
case "$shell" in
|
|
fish ) echo ". \"$(pyenv-prefix "${versions}")/bin/activate.fish\"" ;;
|
|
* ) echo "source \"$(pyenv-prefix "${versions}")/bin/activate\"" ;;
|
|
esac
|