mirror of
https://github.com/pyenv/pyenv-virtualenv.git
synced 2025-11-08 11:33:55 -05:00
Compare commits
22 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9a52f9fb5d | ||
|
|
0ca01826e8 | ||
|
|
4fa9c7b220 | ||
|
|
2571a2b973 | ||
|
|
95fa7a35ad | ||
|
|
47cd86a165 | ||
|
|
d9c3f9a2f5 | ||
|
|
6bc0253d17 | ||
|
|
c184c01174 | ||
|
|
55189357e2 | ||
|
|
9544c8b178 | ||
|
|
bcb7abdf93 | ||
|
|
5953ddaf64 | ||
|
|
2ab9483116 | ||
|
|
32e5a73345 | ||
|
|
0fc9441f09 | ||
|
|
0fe9aef635 | ||
|
|
e04a6f88fe | ||
|
|
08c2e6aee4 | ||
|
|
cc7c9d22b5 | ||
|
|
240a1de6e5 | ||
|
|
9d71e67401 |
15
README.md
15
README.md
@@ -125,11 +125,10 @@ You can also activate and deactivate a pyenv virtualenv manually:
|
||||
Removing the directory in `~/.pyenv/versions` will delete the virtualenv, or you can run:
|
||||
|
||||
pyenv uninstall my-virtual-env
|
||||
|
||||
|
||||
### virtualenv and pyvenv
|
||||
|
||||
There is a [venv](http://docs.python.jp/3/library/venv.html) module available
|
||||
There is a [venv](http://docs.python.org/3/library/venv.html) module available
|
||||
for CPython 3.3 and newer.
|
||||
It provides a command-line tool `pyvenv` which is the successor of `virtualenv`
|
||||
and distributed by default.
|
||||
@@ -157,6 +156,18 @@ You can set certain environment variables to control pyenv-virtualenv.
|
||||
|
||||
## Version History
|
||||
|
||||
#### 20150526
|
||||
|
||||
* Use `typeset -g` with `precmd_functions` (#75)
|
||||
* activate: display setup instructions only with `PYENV_VIRTUALENV_INIT=0` (#78)
|
||||
* Ignore failure of pyenv activate (#68)
|
||||
|
||||
#### 20150119
|
||||
|
||||
* Ignore errors from `pyenv-version-name` since it might fail if there is configuration error (yyuu/pyenv#291)
|
||||
* The _shell_ version set in `activate` should be unset in `deactivate` (#61)
|
||||
* Anaconda has `activate` script nevertheless it is not a virtual environment (#65)
|
||||
|
||||
#### 20141106
|
||||
|
||||
* Stop creating after `ensurepip` since it has done by `ensurepip` itself
|
||||
|
||||
@@ -68,27 +68,11 @@ if ! pyenv-virtualenv-prefix "${versions}" 1>/dev/null 2>&1; then
|
||||
exit 1
|
||||
fi
|
||||
|
||||
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
|
||||
shell="${PYENV_SHELL:-${SHELL##*/}}"
|
||||
|
||||
# Display setup instruction if 'pyenv virtualenv-init -' is not found in "$profile"
|
||||
if [ -f "$profile" ] && grep -q 'pyenv init -' "$profile" && ! grep -q 'pyenv virtualenv-init -' "$profile"; then
|
||||
# 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
|
||||
|
||||
|
||||
@@ -2,14 +2,14 @@
|
||||
#
|
||||
# Summary: Create a Python virtualenv using the pyenv-virtualenv plugin
|
||||
#
|
||||
# Usage: pyenv virtualenv [-f|--force] [VIRTUALENV_OPTIONS] <version> <virtualenv-name>
|
||||
# Usage: pyenv virtualenv [-f|--force] [VIRTUALENV_OPTIONS] [version] <virtualenv-name>
|
||||
# pyenv virtualenv --version
|
||||
# pyenv virtualenv --help
|
||||
#
|
||||
# -f/--force Install even if the version appears to be installed already
|
||||
#
|
||||
|
||||
PYENV_VIRTUALENV_VERSION="20141106"
|
||||
PYENV_VIRTUALENV_VERSION="20150526"
|
||||
|
||||
set -e
|
||||
[ -n "$PYENV_DEBUG" ] && set -x
|
||||
@@ -283,7 +283,7 @@ fi
|
||||
# Set VERSION_NAME as default version in this script
|
||||
export PYENV_VERSION="${VERSION_NAME}"
|
||||
|
||||
# Source version must exists before creating virtualenv
|
||||
# Source version must exist before creating virtualenv.
|
||||
pyenv-prefix 1>/dev/null 2>&1 || usage 1
|
||||
|
||||
if [ -z "$TMPDIR" ]; then
|
||||
@@ -328,12 +328,12 @@ else
|
||||
fi
|
||||
fi
|
||||
|
||||
# Unset environment variables which starts with `VIRTUALENV_`.
|
||||
# Unset environment variables which start with `VIRTUALENV_`.
|
||||
# These variables are reserved for virtualenv.
|
||||
unset VIRTUALENV_VERSION
|
||||
|
||||
|
||||
# Download specified version of ez_setup.py/get-pip.py
|
||||
# Download specified version of ez_setup.py/get-pip.py.
|
||||
if [ -n "${SETUPTOOLS_VERSION}" ]; then
|
||||
EZ_SETUP_URL="https://bitbucket.org/pypa/setuptools/raw/${SETUPTOOLS_VERSION}/ez_setup.py"
|
||||
fi
|
||||
@@ -396,19 +396,19 @@ trap cleanup SIGINT
|
||||
|
||||
# Invoke virtualenv and record exit status in $STATUS.
|
||||
STATUS=0
|
||||
# virtualenv may download distribute/setuptools in current directory.
|
||||
# change to cache directory to reuse them between invocation.
|
||||
# virtualenv may download distribute/setuptools into the current directory.
|
||||
# Change to cache directory to reuse them between invocations.
|
||||
mkdir -p "${PYENV_VIRTUALENV_CACHE_PATH}"
|
||||
cd "${PYENV_VIRTUALENV_CACHE_PATH}"
|
||||
venv $QUIET $VERBOSE "${VIRTUALENV_OPTIONS[@]}" "${VIRTUALENV_PATH}" || STATUS="$?"
|
||||
|
||||
## Install setuptools and pip
|
||||
## Install setuptools and pip.
|
||||
PYENV_VERSION="${VIRTUALENV_NAME}" build_package_ensurepip
|
||||
|
||||
## Migrate previously installed packages from requirements.txt
|
||||
## Migrate previously installed packages from requirements.txt.
|
||||
PYENV_VERSION="${VIRTUALENV_NAME}" install_requirements || true
|
||||
|
||||
# Execute `after_virtualenv` hooks
|
||||
# Execute `after_virtualenv` hooks.
|
||||
for hook in "${after_hooks[@]}"; do eval "$hook"; done
|
||||
|
||||
# Run `pyenv-rehash` after a successful installation.
|
||||
|
||||
@@ -18,7 +18,7 @@ do
|
||||
fi
|
||||
done
|
||||
|
||||
shell="$1"
|
||||
shell="${1:-$PYENV_SHELL}"
|
||||
if [ -z "$shell" ]; then
|
||||
shell="$(ps c -p "$PPID" -o 'ucomm=' 2>/dev/null || true)"
|
||||
shell="${shell##-}"
|
||||
@@ -75,13 +75,14 @@ case "$shell" in
|
||||
fish )
|
||||
cat <<EOS
|
||||
function _pyenv_virtualenv_hook --on-event fish_prompt;
|
||||
set -l PYENV_PREFIX (pyenv prefix 2>/dev/null; or true)
|
||||
if [ -n "\$PYENV_ACTIVATE" ]
|
||||
if [ (pyenv version-name 2>/dev/null; or true) = "system" ]
|
||||
if [ "(pyenv version-name 2>/dev/null; or true)" = "system" ]
|
||||
pyenv deactivate --no-error --verbose
|
||||
set -e PYENV_DEACTIVATE
|
||||
return 0
|
||||
end
|
||||
if [ "\$PYENV_ACTIVATE" != (pyenv prefix 2>/dev/null; or true) ]
|
||||
if [ "\$PYENV_ACTIVATE" != "\$PYENV_PREFIX" ]
|
||||
if pyenv deactivate --no-error --verbose
|
||||
set -e PYENV_DEACTIVATE
|
||||
pyenv activate --no-error --verbose; or set -e PYENV_DEACTIVATE
|
||||
@@ -90,8 +91,8 @@ function _pyenv_virtualenv_hook --on-event fish_prompt;
|
||||
end
|
||||
end
|
||||
else
|
||||
if [ -z "\$VIRTUAL_ENV" ]; and [ "\$PYENV_DEACTIVATE" != (pyenv prefix 2>/dev/null; or true) ]
|
||||
pyenv activate --no-error --verbose
|
||||
if [ -z "\$VIRTUAL_ENV" ]; and [ "\$PYENV_DEACTIVATE" != "\$PYENV_PREFIX" ]
|
||||
pyenv activate --no-error --verbose; or true
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -127,7 +128,7 @@ if [[ "$shell" != "fish" ]]; then
|
||||
fi
|
||||
else
|
||||
if [ -z "\$VIRTUAL_ENV" ] && [ "\$PYENV_DEACTIVATE" != "\$(pyenv prefix 2>/dev/null || true)" ]; then
|
||||
pyenv activate --no-error --verbose
|
||||
pyenv activate --no-error --verbose || true
|
||||
fi
|
||||
fi
|
||||
};
|
||||
@@ -143,7 +144,7 @@ EOS
|
||||
;;
|
||||
zsh )
|
||||
cat <<EOS
|
||||
typeset -a precmd_functions
|
||||
typeset -g -a precmd_functions
|
||||
if [[ -z \$precmd_functions[(r)_pyenv_virtualenv_hook] ]]; then
|
||||
precmd_functions+=_pyenv_virtualenv_hook;
|
||||
fi
|
||||
|
||||
@@ -38,8 +38,14 @@ for version in "${versions[@]}"; do
|
||||
fi
|
||||
PREFIX="$(pyenv-prefix "${version}")"
|
||||
if [ -f "${PREFIX}/bin/activate" ]; then
|
||||
VIRTUALENV_PREFIX_PATH="$(real_prefix "${version}" || base_prefix "${version}" || true)"
|
||||
VIRTUALENV_PREFIX_PATHS=("${VIRTUALENV_PREFIX_PATHS[@]}" "$VIRTUALENV_PREFIX_PATH")
|
||||
# Anaconda has `activate` script nevertheless it is not a virtual environment (#65)
|
||||
if [ -f "${PREFIX}/bin/conda" ]; then
|
||||
echo "pyenv-virtualenv: version \`${version}' is an anaconda/miniconda" 1>&2
|
||||
exit 1
|
||||
else
|
||||
VIRTUALENV_PREFIX_PATH="$(real_prefix "${version}" || base_prefix "${version}" || true)"
|
||||
VIRTUALENV_PREFIX_PATHS=("${VIRTUALENV_PREFIX_PATHS[@]}" "$VIRTUALENV_PREFIX_PATH")
|
||||
fi
|
||||
else
|
||||
echo "pyenv-virtualenv: version \`${version}' is not a virtualenv" 1>&2
|
||||
exit 1
|
||||
|
||||
@@ -65,6 +65,11 @@ EOS
|
||||
|
||||
assert_success
|
||||
assert_output <<EOS
|
||||
# Load pyenv-virtualenv automatically by adding
|
||||
# the following to ~/.bash_profile:
|
||||
|
||||
eval "\$(pyenv virtualenv-init -)"
|
||||
|
||||
pyenv shell "venv";
|
||||
export PYENV_ACTIVATE_SHELL=1;
|
||||
unset PYENV_DEACTIVATE;
|
||||
@@ -109,6 +114,11 @@ EOS
|
||||
|
||||
assert_success
|
||||
assert_output <<EOS
|
||||
# Load pyenv-virtualenv automatically by adding
|
||||
# the following to ~/.config/fish/config.fish:
|
||||
|
||||
status --is-interactive; and . (pyenv virtualenv-init -|psub)
|
||||
|
||||
pyenv shell "venv";
|
||||
setenv PYENV_ACTIVATE_SHELL 1;
|
||||
set -e PYENV_DEACTIVATE;
|
||||
@@ -123,7 +133,7 @@ EOS
|
||||
stub pyenv-virtualenv-prefix "venv27 : echo \"${PYENV_ROOT}/versions/venv27\""
|
||||
stub pyenv-prefix "venv27 : echo \"${PYENV_ROOT}/versions/venv27\""
|
||||
|
||||
run pyenv-sh-activate "venv27"
|
||||
PYENV_SHELL="bash" PYENV_VERSION="venv" run pyenv-sh-activate "venv27"
|
||||
|
||||
unstub pyenv-virtualenv-prefix
|
||||
unstub pyenv-prefix
|
||||
@@ -144,7 +154,33 @@ EOS
|
||||
stub pyenv-virtualenv-prefix "venv27 : echo \"${PYENV_ROOT}/versions/venv27\""
|
||||
stub pyenv-prefix "venv27 : echo \"${PYENV_ROOT}/versions/venv27\""
|
||||
|
||||
run pyenv-sh-activate "venv27"
|
||||
PYENV_SHELL="bash" PYENV_VERSION="venv" run pyenv-sh-activate "venv27"
|
||||
|
||||
unstub pyenv-virtualenv-prefix
|
||||
unstub pyenv-prefix
|
||||
|
||||
assert_success
|
||||
assert_output <<EOS
|
||||
# Load pyenv-virtualenv automatically by adding
|
||||
# the following to ~/.bash_profile:
|
||||
|
||||
eval "\$(pyenv virtualenv-init -)"
|
||||
|
||||
pyenv shell "venv27";
|
||||
export PYENV_ACTIVATE_SHELL=1;
|
||||
unset PYENV_DEACTIVATE;
|
||||
export PYENV_ACTIVATE="${PYENV_ROOT}/versions/venv27";
|
||||
. "${PYENV_ROOT}/versions/venv27/bin/activate";
|
||||
EOS
|
||||
}
|
||||
|
||||
@test "activate virtualenv from command-line argument (fish)" {
|
||||
export PYENV_VIRTUALENV_INIT=1
|
||||
|
||||
stub pyenv-virtualenv-prefix "venv27 : echo \"${PYENV_ROOT}/versions/venv27\""
|
||||
stub pyenv-prefix "venv27 : echo \"${PYENV_ROOT}/versions/venv27\""
|
||||
|
||||
PYENV_SHELL="fish" PYENV_VERSION="venv" run pyenv-sh-activate "venv27"
|
||||
|
||||
unstub pyenv-virtualenv-prefix
|
||||
unstub pyenv-prefix
|
||||
@@ -152,10 +188,36 @@ EOS
|
||||
assert_success
|
||||
assert_output <<EOS
|
||||
pyenv shell "venv27";
|
||||
export PYENV_ACTIVATE_SHELL=1;
|
||||
unset PYENV_DEACTIVATE;
|
||||
export PYENV_ACTIVATE="${PYENV_ROOT}/versions/venv27";
|
||||
. "${PYENV_ROOT}/versions/venv27/bin/activate";
|
||||
setenv PYENV_ACTIVATE_SHELL 1;
|
||||
set -e PYENV_DEACTIVATE;
|
||||
setenv PYENV_ACTIVATE "${PYENV_ROOT}/versions/venv27";
|
||||
. "${PYENV_ROOT}/versions/venv27/bin/activate.fish";
|
||||
EOS
|
||||
}
|
||||
|
||||
@test "activate virtualenv from command-line argument (fish) (without pyenv-virtualenv-init)" {
|
||||
export PYENV_VIRTUALENV_INIT=
|
||||
|
||||
stub pyenv-virtualenv-prefix "venv27 : echo \"${PYENV_ROOT}/versions/venv27\""
|
||||
stub pyenv-prefix "venv27 : echo \"${PYENV_ROOT}/versions/venv27\""
|
||||
|
||||
PYENV_SHELL="fish" PYENV_VERSION="venv" run pyenv-sh-activate "venv27"
|
||||
|
||||
unstub pyenv-virtualenv-prefix
|
||||
unstub pyenv-prefix
|
||||
|
||||
assert_success
|
||||
assert_output <<EOS
|
||||
# Load pyenv-virtualenv automatically by adding
|
||||
# the following to ~/.config/fish/config.fish:
|
||||
|
||||
status --is-interactive; and . (pyenv virtualenv-init -|psub)
|
||||
|
||||
pyenv shell "venv27";
|
||||
setenv PYENV_ACTIVATE_SHELL 1;
|
||||
set -e PYENV_DEACTIVATE;
|
||||
setenv PYENV_ACTIVATE "${PYENV_ROOT}/versions/venv27";
|
||||
. "${PYENV_ROOT}/versions/venv27/bin/activate.fish";
|
||||
EOS
|
||||
}
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
load test_helper
|
||||
|
||||
@test "detect parent shell" {
|
||||
unset PYENV_SHELL
|
||||
root="$(cd $BATS_TEST_DIRNAME/.. && pwd)"
|
||||
SHELL=/bin/false run pyenv-virtualenv-init -
|
||||
assert_success
|
||||
@@ -47,7 +48,7 @@ _pyenv_virtualenv_hook() {
|
||||
fi
|
||||
else
|
||||
if [ -z "\$VIRTUAL_ENV" ] && [ "\$PYENV_DEACTIVATE" != "\$(pyenv prefix 2>/dev/null || true)" ]; then
|
||||
pyenv activate --no-error --verbose
|
||||
pyenv activate --no-error --verbose || true
|
||||
fi
|
||||
fi
|
||||
};
|
||||
@@ -63,13 +64,14 @@ EOS
|
||||
assert_output <<EOS
|
||||
setenv PYENV_VIRTUALENV_INIT 1;
|
||||
function _pyenv_virtualenv_hook --on-event fish_prompt;
|
||||
set -l PYENV_PREFIX (pyenv prefix 2>/dev/null; or true)
|
||||
if [ -n "\$PYENV_ACTIVATE" ]
|
||||
if [ (pyenv version-name 2>/dev/null; or true) = "system" ]
|
||||
if [ "(pyenv version-name 2>/dev/null; or true)" = "system" ]
|
||||
pyenv deactivate --no-error --verbose
|
||||
set -e PYENV_DEACTIVATE
|
||||
return 0
|
||||
end
|
||||
if [ "\$PYENV_ACTIVATE" != (pyenv prefix 2>/dev/null; or true) ]
|
||||
if [ "\$PYENV_ACTIVATE" != "\$PYENV_PREFIX" ]
|
||||
if pyenv deactivate --no-error --verbose
|
||||
set -e PYENV_DEACTIVATE
|
||||
pyenv activate --no-error --verbose; or set -e PYENV_DEACTIVATE
|
||||
@@ -78,8 +80,8 @@ function _pyenv_virtualenv_hook --on-event fish_prompt;
|
||||
end
|
||||
end
|
||||
else
|
||||
if [ -z "\$VIRTUAL_ENV" ]; and [ "\$PYENV_DEACTIVATE" != (pyenv prefix 2>/dev/null; or true) ]
|
||||
pyenv activate --no-error --verbose
|
||||
if [ -z "\$VIRTUAL_ENV" ]; and [ "\$PYENV_DEACTIVATE" != "\$PYENV_PREFIX" ]
|
||||
pyenv activate --no-error --verbose; or true
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -108,11 +110,11 @@ _pyenv_virtualenv_hook() {
|
||||
fi
|
||||
else
|
||||
if [ -z "\$VIRTUAL_ENV" ] && [ "\$PYENV_DEACTIVATE" != "\$(pyenv prefix 2>/dev/null || true)" ]; then
|
||||
pyenv activate --no-error --verbose
|
||||
pyenv activate --no-error --verbose || true
|
||||
fi
|
||||
fi
|
||||
};
|
||||
typeset -a precmd_functions
|
||||
typeset -g -a precmd_functions
|
||||
if [[ -z \$precmd_functions[(r)_pyenv_virtualenv_hook] ]]; then
|
||||
precmd_functions+=_pyenv_virtualenv_hook;
|
||||
fi
|
||||
|
||||
@@ -4,7 +4,7 @@ load test_helper
|
||||
|
||||
setup() {
|
||||
export PYENV_ROOT="${TMP}/pyenv"
|
||||
export PYENV_VIRTUALENV_VERSION="20141106"
|
||||
export PYENV_VIRTUALENV_VERSION="20150526"
|
||||
}
|
||||
|
||||
@test "display virtualenv version" {
|
||||
|
||||
Reference in New Issue
Block a user