Skip to content

Commit

Permalink
Include PYTHON_VERSION when running pyenv virtualenv
Browse files Browse the repository at this point in the history
If PYTHON_VERSION is an empty string then the system Python will be used.
  • Loading branch information
jsf9k committed Jan 26, 2024
1 parent e1d0f28 commit 517b336
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions setup-env
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ FORCE=0
# Positional parameters
PARAMS=""

# Flags to allow a user to specify which version of Python they want to use
PYTHON_VERSION=""
# A flag to allow a user to specify which version of Python they want
# to use.
LIST_VERSIONS=0

# Parse command line arguments
Expand Down Expand Up @@ -132,8 +132,9 @@ if [ $LIST_VERSIONS -ne 0 ]; then
read -r -p "Enter the desired Python version: " PYTHON_VERSION
fi

# Check if PYTHON_VERSION isn't empty. If it is installed, set it locally.
if [ -n "$PYTHON_VERSION" ]; then
# Check if PYTHON_VERSION is defined. If it is defined then check that
# it is a valid value.
if [ -n "${PYTHON_VERSION+x}" ]; then
if pyenv versions --bare --skip-aliases --skip-envs | grep --fixed-strings "$PYTHON_VERSION" > /dev/null; then
echo Using Python version "$PYTHON_VERSION"
else
Expand All @@ -159,7 +160,15 @@ END_OF_LINE
fi

# Create a new virtual environment for this project
if ! pyenv virtualenv "${env_name}"; then
#
# If $PYTHON_VERSION is undefined then the system Python will be used.
#
# We can't quote ${PYTHON_VERSION:=} below since if the variable is
# undefined then we want nothing to appear; this is the reason for the
# "shellcheck disable" line below.
#
# shellcheck disable=SC2086
if ! pyenv virtualenv ${PYTHON_VERSION:=} "${env_name}"; then
cat << END_OF_LINE
An existing virtual environment named $env_name was found. Either delete this
environment yourself or re-run with --force option to have it deleted.
Expand Down

0 comments on commit 517b336

Please sign in to comment.