diff --git a/README.md b/README.md index ea739f39..18e785ce 100644 --- a/README.md +++ b/README.md @@ -56,6 +56,7 @@ This project was forked from [rbenv](https://github.com/rbenv/rbenv) and * [Install additional Python versions](#install-additional-python-versions) * [Prefix auto-resolution to the latest version](#prefix-auto-resolution-to-the-latest-version) * [Python versions with extended support](#python-versions-with-extended-support) + * [Install a named python version](#intstall-a-named-python-version) * [Switch between Python versions](#switch-between-python-versions) * [Uninstall Python versions](#uninstall-python-versions) * [Other operations](#other-operations) @@ -445,6 +446,20 @@ in a later version of those environments. * *2.7.18* : MacOS 10.15+ and Apple Silicon +#### Install a named python version + +To install a named python version, the `--name ` argument can be given when installing a new version. This will +append a `-` string to the end of the version being installed. The `name` argument can only contain alpha-numeric, +`-`, and `_` characters. + +E.g. to install and switch to a 3.11.3 version named `test-environment` + +```sh +pyenv install --name test-environment 3.11.3 +pyenv global 3.11.3-test-environment +``` + + ### Switch between Python versions To select a Pyenv-installed Python as the version to use, run one diff --git a/plugins/python-build/bin/pyenv-install b/plugins/python-build/bin/pyenv-install index 2d5c356b..86203eeb 100755 --- a/plugins/python-build/bin/pyenv-install +++ b/plugins/python-build/bin/pyenv-install @@ -19,6 +19,7 @@ # -v/--verbose Verbose mode: print compilation status to stdout # --version Show version of python-build # -g/--debug Build a debug version +# -n/--name Build named version as - (name: cannot contain spaces) # # For detailed information on installing Python versions with # python-build, including a list of environment variables for adjusting @@ -46,6 +47,7 @@ if [ "$1" = "--complete" ]; then echo --verbose echo --version echo --debug + echo --name exec python-build --definitions fi @@ -104,6 +106,11 @@ for option in "${OPTIONS[@]}"; do "g" | "debug" ) DEBUG="-g" ;; + "n" | "name" ) + [[ "${#ARGUMENTS[*]}" -eq 0 ]] && echo -e "Error: Expected argument for --name\n" >&2 && usage 1 >&2 + NAME=${ARGUMENTS[0]} + unset ARGUMENTS[0] + ;; "version" ) exec python-build --version ;; @@ -123,6 +130,12 @@ DEFINITIONS=("${ARGUMENTS[@]}") [[ "${#DEFINITIONS[*]}" -eq 0 ]] && DEFINITIONS=($(pyenv-local 2>/dev/null || true)) [[ "${#DEFINITIONS[*]}" -eq 0 ]] && usage 1 >&2 +# If the `name` argument has been used, validate the name given is valid +if [ ! $(grep '^[-0-9a-zA-Z_]*$' <<< $NAME) ]; then + echo -e "Error: --name argument can only contain alpha-numeric, '-', and '_' characters\n" >&2 + usage 1 >&2 +fi + # Define `before_install` and `after_install` functions that allow # plugin hooks to register a string of code for execution before or # after the installation process. @@ -163,6 +176,7 @@ for DEFINITION in "${DEFINITIONS[@]}"; do # Set VERSION_NAME from $DEFINITION. Then compute the installation prefix. VERSION_NAME="${DEFINITION##*/}" [ -n "$DEBUG" ] && VERSION_NAME="${VERSION_NAME}-debug" + [ -n "$NAME" ] && VERSION_NAME="${VERSION_NAME}-${NAME}" PREFIX="${PYENV_ROOT}/versions/${VERSION_NAME}" [ -d "${PREFIX}" ] && PREFIX_EXISTS=1