diff --git a/README.md b/README.md index 8cb5762..271d49f 100644 --- a/README.md +++ b/README.md @@ -78,6 +78,23 @@ version. venv33 (created from /home/yyuu/.pyenv/versions/3.3.3) +### Activate virtualenv + +Some external tools (e.g. [jedi](https://github.com/davidhalter/jedi)) might require you to `activate` the virtualenv. +`pyenv activate` lets you to activate the virtualenv into your shell. + + $ pyenv activate venv27 + +`pyenv activate` acts almost like following commands. + + $ pyenv shell venv27 + $ source "$(pyenv prefix venv27)/bin/activate" + +You can deactivate the activate'd virtualenv by `pyenv deactivate`. + + $ pyenv deactivate + + ### Special environment variables You can set certain environment variables to control the pyenv-virtualenv. diff --git a/bin/pyenv-sh-activate b/bin/pyenv-sh-activate new file mode 100755 index 0000000..33f2352 --- /dev/null +++ b/bin/pyenv-sh-activate @@ -0,0 +1,48 @@ +#!/usr/bin/env bash +# +# Summary: Activate virtual environment +# +# Usage: pyenv activate +# 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. +# +# 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 + +versions=("$@") +shell="$(basename "${PYENV_SHELL:-$SHELL}")" + +if [ -z "$versions" ]; then + OLDIFS="$IFS" + IFS=: versions=($(pyenv-version-name)) + IFS="$OLDIFS" +fi + +if [ "$1" = "--unset" ]; then + echo "pyenv deactivate" + exit +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 + +echo "pyenv shell \"${versions}\"" +case "$shell" in +fish ) echo ". \"$(pyenv-prefix "${versions}")/bin/activate.fish\"" ;; +* ) echo "source \"$(pyenv-prefix "${versions}")/bin/activate\"" ;; +esac diff --git a/bin/pyenv-sh-deactivate b/bin/pyenv-sh-deactivate new file mode 100755 index 0000000..a0d2dd9 --- /dev/null +++ b/bin/pyenv-sh-deactivate @@ -0,0 +1,17 @@ +#!/usr/bin/env bash +# +# Summary: Deactivate virtual environment +# +# Usage: pyenv deactivate +# +# Deactivate a Python virtual environment. + +set -e +[ -n "$PYENV_DEBUG" ] && set -x + +shell="$(basename "${PYENV_SHELL:-$SHELL}")" +case "$shell" in +fish ) echo "functions -q deactivate; and deactivate";; +* ) echo "declare -f deactivate 1>/dev/null 2>&1 && deactivate";; +esac +echo "pyenv shell --unset" diff --git a/bin/pyenv-virtualenv b/bin/pyenv-virtualenv index ed63e4a..6909b17 100755 --- a/bin/pyenv-virtualenv +++ b/bin/pyenv-virtualenv @@ -230,7 +230,7 @@ for option in "${OPTIONS[@]}"; do done if [[ "${#ARGUMENTS[@]}" == 0 ]]; then - echo "pyenv: no virtualenv name given." 1>&2 + echo "pyenv-virtualenv: no virtualenv name given." 1>&2 exit 1 elif [[ "${#ARGUMENTS[@]}" == 1 ]]; then # If only one argument given, use current version as source version @@ -312,7 +312,7 @@ done # the --force option was specified. if [ -d "${VIRTUALENV_PATH}/bin" ]; then if [ -z "$FORCE" ]; then - echo "pyenv: ${VIRTUALENV_PATH} already exists" 1>&2 + echo "pyenv-virtualenv: ${VIRTUALENV_PATH} already exists" 1>&2 read -p "continue with installation? (y/N) " case "$REPLY" in diff --git a/bin/pyenv-virtualenv-prefix b/bin/pyenv-virtualenv-prefix index a7d49ac..3cc56de 100755 --- a/bin/pyenv-virtualenv-prefix +++ b/bin/pyenv-virtualenv-prefix @@ -39,7 +39,7 @@ for version in "${versions[@]}"; do VIRTUALENV_PREFIX_PATHS=("${VIRTUALENV_PREFIX_PATHS[@]}" "$VIRTUALENV_PREFIX_PATH") fi else - echo "pyenv: version \`${version}' is not a virtualenv" 1>&2 + echo "pyenv-virtualenv: version \`${version}' is not a virtualenv" 1>&2 exit 1 fi done