8 Commits

Author SHA1 Message Date
k
3a4efe6a95 Merge 5d7894d189 into aa9b8e9a5a 2025-01-11 13:21:02 -05:00
Jason N. White
aa9b8e9a5a Update LICENSE, fix license year (#499)
Some checks failed
tests / tests (macos-13) (push) Has been cancelled
tests / tests (macos-14) (push) Has been cancelled
tests / tests (ubuntu-20.04) (push) Has been cancelled
tests / tests (ubuntu-22.04) (push) Has been cancelled
Signed-off-by: JasonnnW3000 <sufssl04@gmail.com>
2025-01-01 20:46:21 +03:00
native-api
e8c8fd9b84 Fix errorneously creating a symlink inside env directory if running with -f (#496)
Some checks failed
tests / tests (macos-13) (push) Has been cancelled
tests / tests (macos-14) (push) Has been cancelled
tests / tests (ubuntu-20.04) (push) Has been cancelled
tests / tests (ubuntu-22.04) (push) Has been cancelled
2024-12-08 23:30:27 +03:00
native-api
db299cada3 Fix confusing activate/deactivate error messages (#495) 2024-12-08 21:43:35 +03:00
native-api
c6bb0694bf Fix pyenv-virtualenv using a different Python version in a conda environment (#492)
Some checks failed
tests / tests (macos-13) (push) Has been cancelled
tests / tests (macos-14) (push) Has been cancelled
tests / tests (ubuntu-20.04) (push) Has been cancelled
tests / tests (ubuntu-22.04) (push) Has been cancelled
Allow output checking with wildcards
2024-11-25 00:54:05 +03:00
Zac Holland
28cd9be54e README: fix and distinguish syntax highlighting in Bash vs Fish snippets (#489)
Some checks failed
tests / tests (macos-13) (push) Has been cancelled
tests / tests (macos-14) (push) Has been cancelled
tests / tests (ubuntu-20.04) (push) Has been cancelled
tests / tests (ubuntu-22.04) (push) Has been cancelled
Co-authored-by: native-api <vano@mail.mipt.ru>
2024-09-18 22:08:52 +03:00
Jovier Jimenez
d4c9655e26 Add prompt customization (#476)
Some checks failed
tests / tests (macos-13) (push) Has been cancelled
tests / tests (macos-14) (push) Has been cancelled
tests / tests (ubuntu-20.04) (push) Has been cancelled
tests / tests (ubuntu-22.04) (push) Has been cancelled
2024-09-15 09:40:44 +03:00
K Kollmann
5d7894d189 fix: PATH update for non-fish shells
Checks PATH variable for existing inclusion of shims
for shells other than fish.

See PR #430
2024-01-20 20:10:27 +01:00
14 changed files with 118 additions and 28 deletions

View File

@@ -1,4 +1,4 @@
Copyright (c) 2015 Yamashita, Yuu
Copyright (c) 2025 Yamashita, Yuu
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the

View File

@@ -30,25 +30,25 @@ From inside that directory you can:
1. **Check out pyenv-virtualenv into plugin directory**
```sh
```bash
git clone https://github.com/pyenv/pyenv-virtualenv.git $(pyenv root)/plugins/pyenv-virtualenv
```
For the Fish shell:
```sh
```fish
git clone https://github.com/pyenv/pyenv-virtualenv.git (pyenv root)/plugins/pyenv-virtualenv
```
2. (OPTIONAL) **Add `pyenv virtualenv-init` to your shell** to enable auto-activation of virtualenvs. This is entirely optional but pretty useful. See "Activate virtualenv" below.
```sh
```bash
echo 'eval "$(pyenv virtualenv-init -)"' >> ~/.bashrc
```
**Fish shell note**: Add this to your `~/.config/fish/config.fish`
```sh
```fish
status --is-interactive; and pyenv virtualenv-init - | source
```
@@ -56,7 +56,7 @@ From inside that directory you can:
3. **Restart your shell to enable pyenv-virtualenv**
```sh
```bash
exec "$SHELL"
```
@@ -243,6 +243,7 @@ You can set certain environment variables to control pyenv-virtualenv.
* `PIP_VERSION`, if set and `venv` is preferred
over `virtualenv`, install the specified version of pip.
* `PYENV_VIRTUALENV_VERBOSE_ACTIVATE`, if set, shows some verbose outputs on activation and deactivation
* `PYENV_VIRTUALENV_PROMPT`, if set, allows users to customize how `pyenv-virtualenv` modifies their shell prompt. The default prompt ("(venv)") is overwritten with any user-specified text. Specify the location of the virtual environment name with the string `{venv}`. For example, the default prompt string would be `({venv})`.
## Version History

View File

@@ -22,10 +22,8 @@ fi
{ printf "\x1B[31;1m"
echo
echo "Failed to activate virtualenv."
echo
echo "Perhaps pyenv-virtualenv has not been loaded into your shell properly."
echo "Please restart current shell and try again."
echo "\`pyenv activate' requires Pyenv and Pyenv-Virtualenv to be loaded into your shell."
echo "Check your shell configuration and Pyenv and Pyenv-Virtualenv installation instructions."
echo
printf "\x1B[0m"
} 1>&2

View File

@@ -11,10 +11,8 @@ set -e
{ printf "\x1B[31;1m"
echo
echo "Failed to deactivate virtualenv."
echo
echo "Perhaps pyenv-virtualenv has not been loaded into your shell properly."
echo "Please restart current shell and try again."
echo "\`pyenv deactivate' requires Pyenv and Pyenv-Virtualenv to be loaded into your shell."
echo "Check your shell configuration and Pyenv and Pyenv-Virtualenv installation instructions."
echo
printf "\x1B[0m"
} 1>&2

View File

@@ -262,9 +262,14 @@ EOS
fi
;;
* )
if [ -z "${PYENV_VIRTUALENV_PROMPT}" ]; then
PYENV_VIRTUALENV_PROMPT="(${venv})"
else
PYENV_VIRTUALENV_PROMPT="${PYENV_VIRTUALENV_PROMPT/\{venv\}/${venv}}"
fi
cat <<EOS
export _OLD_VIRTUAL_PS1="\${PS1:-}";
export PS1="(${venv}) \${PS1:-}";
export PS1="${PYENV_VIRTUALENV_PROMPT} \${PS1:-}";
EOS
;;
esac

View File

@@ -605,7 +605,13 @@ STATUS=0
mkdir -p "${PYENV_VIRTUALENV_CACHE_PATH}"
cd "${PYENV_VIRTUALENV_CACHE_PATH}"
if [ -n "${USE_CONDA}" ]; then
pyenv-exec conda create $QUIET $VERBOSE --name "${VIRTUALENV_PATH##*/}" --yes "${VIRTUALENV_OPTIONS[@]}" python || STATUS="$?"
if [ -z "$VIRTUALENV_PYTHON" ]; then
#process substitution doesn't seem to work unless it's directly inserted to the command line
#if we add it to VIRTUALENV_OPTIONS instead, "broken pipe" happens
pyenv-exec conda create $QUIET $VERBOSE --name "${VIRTUALENV_PATH##*/}" --yes "${VIRTUALENV_OPTIONS[@]}" --file <(pyenv-exec conda list python --full-name --export) || STATUS="$?"
else
pyenv-exec conda create $QUIET $VERBOSE --name "${VIRTUALENV_PATH##*/}" --yes "${VIRTUALENV_OPTIONS[@]}" || STATUS="$?"
fi
else
if [ -n "${USE_M_VENV}" ]; then
pyenv-exec "${M_VENV_PYTHON_BIN:-python}" -m venv $QUIET $VERBOSE "${VIRTUALENV_OPTIONS[@]}" "${VIRTUALENV_PATH}" || STATUS="$?"
@@ -625,7 +631,8 @@ fi
## Create symlink in the `versions` directory for backward compatibility
if [ -d "${VIRTUALENV_PATH}" ] && [ -n "${COMPAT_VIRTUALENV_PATH}" ]; then
ln -fs "${VIRTUALENV_PATH}" "${COMPAT_VIRTUALENV_PATH}"
# Overwrite an existing link, can happen if running with -f
ln -fsn "${VIRTUALENV_PATH}" "${COMPAT_VIRTUALENV_PATH}"
fi
if [ ! -e "${VIRTUALENV_PATH}/bin/pydoc" ]; then

View File

@@ -95,8 +95,10 @@ EOS
;;
* )
cat <<EOS
export PATH="${PYENV_VIRTUALENV_ROOT:-${PYENV_VIRTUALENV_INSTALL_PREFIX}}/shims:\${PATH}";
export PYENV_VIRTUALENV_INIT=1;
if [[ ":$PATH:" != *"${PYENV_VIRTUALENV_ROOT:-${PYENV_VIRTUALENV_INSTALL_PREFIX}}/shims"* ]]; then
export PATH="${PYENV_VIRTUALENV_ROOT:-${PYENV_VIRTUALENV_INSTALL_PREFIX}}/shims:\${PATH}";
export PYENV_VIRTUALENV_INIT=1;
fi
EOS
;;
esac

View File

@@ -15,6 +15,7 @@ setup() {
unset PYENV_VIRTUALENV_DISABLE_PROMPT
unset PYENV_VIRTUAL_ENV_DISABLE_PROMPT
unset VIRTUAL_ENV_DISABLE_PROMPT
unset PYENV_VIRTUALENV_PROMPT
unset _OLD_VIRTUAL_PS1
stub pyenv-hooks "activate : echo"
}
@@ -44,6 +45,31 @@ EOS
unstub pyenv-sh-deactivate
}
@test "activate virtualenv from current version with custom prompt" {
export PYENV_VIRTUALENV_INIT=1
stub pyenv-version-name "echo venv"
stub pyenv-virtualenv-prefix "venv : echo \"${PYENV_ROOT}/versions/venv\""
stub pyenv-prefix "venv : echo \"${PYENV_ROOT}/versions/venv\""
stub pyenv-sh-deactivate "--force --quiet : echo deactivated"
PYENV_SHELL="bash" PYENV_VERSION="venv" PYENV_VIRTUALENV_PROMPT='venv:{venv}' run pyenv-sh-activate
assert_success
assert_output <<EOS
deactivated
export PYENV_VIRTUAL_ENV="${PYENV_ROOT}/versions/venv";
export VIRTUAL_ENV="${PYENV_ROOT}/versions/venv";
export _OLD_VIRTUAL_PS1="\${PS1:-}";
export PS1="venv:venv \${PS1:-}";
EOS
unstub pyenv-version-name
unstub pyenv-virtualenv-prefix
unstub pyenv-prefix
unstub pyenv-sh-deactivate
}
@test "activate virtualenv from current version (quiet)" {
export PYENV_VIRTUALENV_INIT=1

View File

@@ -16,6 +16,7 @@ setup() {
unset PYENV_VIRTUALENV_DISABLE_PROMPT
unset PYENV_VIRTUAL_ENV_DISABLE_PROMPT
unset VIRTUAL_ENV_DISABLE_PROMPT
unset PYENV_VIRTUALENV_PROMPT
unset _OLD_VIRTUAL_PS1
stub pyenv-hooks "activate : echo"
}
@@ -53,6 +54,35 @@ EOS
teardown_conda "anaconda-2.3.0"
}
@test "activate conda root from current version with custom prompt" {
export PYENV_VIRTUALENV_INIT=1
setup_conda "anaconda-2.3.0"
stub pyenv-version-name "echo anaconda-2.3.0"
stub pyenv-virtualenv-prefix "anaconda-2.3.0 : echo \"${PYENV_ROOT}/versions/anaconda-2.3.0\""
stub pyenv-prefix "anaconda-2.3.0 : echo \"${PYENV_ROOT}/versions/anaconda-2.3.0\""
stub pyenv-sh-deactivate "--force --quiet : echo deactivated"
PYENV_SHELL="bash" PYENV_VERSION="anaconda-2.3.0" PYENV_VIRTUALENV_PROMPT='venv:{venv}' run pyenv-sh-activate
assert_success
assert_output <<EOS
deactivated
export PYENV_VIRTUAL_ENV="${PYENV_ROOT}/versions/anaconda-2.3.0";
export VIRTUAL_ENV="${PYENV_ROOT}/versions/anaconda-2.3.0";
export CONDA_DEFAULT_ENV="root";
export _OLD_VIRTUAL_PS1="\${PS1:-}";
export PS1="venv:anaconda-2.3.0 \${PS1:-}";
export CONDA_PREFIX="${TMP}/pyenv/versions/anaconda-2.3.0";
EOS
unstub pyenv-version-name
unstub pyenv-virtualenv-prefix
unstub pyenv-prefix
unstub pyenv-sh-deactivate
teardown_conda "anaconda-2.3.0"
}
@test "activate conda root from current version (fish)" {
export PYENV_VIRTUALENV_INIT=1

View File

@@ -32,8 +32,8 @@ unstub_pyenv() {
run pyenv-virtualenv venv
assert_success
assert_output <<OUT
PYENV_VERSION=miniconda3-3.16.0 conda create --name venv --yes python
assert_output_wildcards <<OUT
PYENV_VERSION=miniconda3-3.16.0 conda create --name venv --yes --file /dev/fd/*
rehashed
OUT
@@ -56,7 +56,7 @@ OUT
assert_success
assert_output <<OUT
PYENV_VERSION=miniconda3-3.16.0 conda create --name venv --yes python=3.5 python
PYENV_VERSION=miniconda3-3.16.0 conda create --name venv --yes python=3.5
rehashed
OUT
@@ -79,7 +79,7 @@ OUT
assert_success
assert_output <<OUT
PYENV_VERSION=miniconda3-3.16.0 conda create --name venv --yes python=3.5 python
PYENV_VERSION=miniconda3-3.16.0 conda create --name venv --yes python=3.5
rehashed
OUT

View File

@@ -1,4 +1,4 @@
#!/usr/bin/env bats
#!/usr/bin/env bats
load test_helper

View File

@@ -6,6 +6,7 @@ setup() {
export PYENV_ROOT="${TMP}/pyenv"
export HOOK_PATH="${TMP}/i has hooks"
mkdir -p "$HOOK_PATH"
unset PYENV_VIRTUALENV_PROMPT
}
@test "pyenv-virtualenv hooks" {

View File

@@ -50,8 +50,10 @@ load test_helper
run pyenv-virtualenv-init - bash
assert_success
assert_output <<EOS
export PATH="${TMP}/pyenv/plugins/pyenv-virtualenv/shims:\${PATH}";
export PYENV_VIRTUALENV_INIT=1;
if [[ ":$PATH:" != *"${TMP}/pyenv/plugins/pyenv-virtualenv/shims"* ]]; then
export PATH="${TMP}/pyenv/plugins/pyenv-virtualenv/shims:\${PATH}";
export PYENV_VIRTUALENV_INIT=1;
fi
_pyenv_virtualenv_hook() {
local ret=\$?
if [ -n "\${VIRTUAL_ENV-}" ]; then
@@ -93,8 +95,10 @@ EOS
run pyenv-virtualenv-init - zsh
assert_success
assert_output <<EOS
export PATH="${TMP}/pyenv/plugins/pyenv-virtualenv/shims:\${PATH}";
export PYENV_VIRTUALENV_INIT=1;
if [[ ":$PATH:" != *"${TMP}/pyenv/plugins/pyenv-virtualenv/shims"* ]]; then
export PATH="${TMP}/pyenv/plugins/pyenv-virtualenv/shims:\${PATH}";
export PYENV_VIRTUALENV_INIT=1;
fi
_pyenv_virtualenv_hook() {
local ret=\$?
if [ -n "\${VIRTUAL_ENV-}" ]; then

View File

@@ -83,6 +83,16 @@ assert_equal() {
fi
}
assert_equal_wildcards() {
if [[ $1 != $2 ]]; then
{ echo "expected:"
echo "$2"
echo "actual:"
echo "$1"
} | flunk
fi
}
assert_output() {
local expected
if [ $# -eq 0 ]; then expected="$(cat -)"
@@ -91,6 +101,14 @@ assert_output() {
assert_equal "$expected" "$output"
}
assert_output_wildcards() {
local expected
if [ $# -eq 0 ]; then expected="$(cat -)"
else expected="$1"
fi
assert_equal_wildcards "$output" "$expected"
}
assert_output_contains() {
local expected="$1"
echo "$output" | grep -F "$expected" >/dev/null || {