| 1 | #!/usr/bin/env bash |
| 2 | # |
| 3 | # Summary: Set or show the local application-specific Python version(s) |
| 4 | # |
| 5 | # Usage: pyenv local [-f|--force] [<version> [...]] |
| 6 | # pyenv local --unset |
| 7 | # |
| 8 | # -f/--force Do not verify that the versions being set exist |
| 9 | # |
| 10 | # Sets the local application-specific Python version(s) by writing the |
| 11 | # version name to a file named `.python-version'. |
| 12 | # |
| 13 | # When you run a Python command, pyenv will look for a `.python-version' |
| 14 | # file in the current directory and each parent directory. If no such |
| 15 | # file is found in the tree, pyenv will use the global Python version |
| 16 | # specified with `pyenv global'. A version specified with the |
| 17 | # `PYENV_VERSION' environment variable takes precedence over local |
| 18 | # and global versions. |
| 19 | # |
| 20 | # <version> can be specified multiple times and should be a version |
| 21 | # tag known to pyenv. The special version string `system' will use |
| 22 | # your default system Python. Run `pyenv versions' for a list of |
| 23 | # available Python versions. |
| 24 | # |
| 25 | # Example: To enable the python2.7 and python3.7 shims to find their |
| 26 | # respective executables you could set both versions with: |
| 27 | # |
| 28 | # 'pyenv local 3.7.0 2.7.15' |
| 29 | |
| 30 | |
| 31 | set -e |
| 32 | [ -n "$PYENV_DEBUG" ] && set -x |
| 33 | |
| 34 | # Provide pyenv completions |
| 35 | if [ "$1" = "--complete" ]; then |
| 36 | echo --unset |
| 37 | echo system |
| 38 | exec pyenv-versions --bare |
| 39 | fi |
| 40 | |
| 41 | while [[ $# -gt 0 ]] |
| 42 | do |
| 43 | case "$1" in |
| 44 | -f|--force) |
| 45 | FORCE=1 |
| 46 | shift |
| 47 | ;; |
| 48 | *) |
| 49 | break |
| 50 | ;; |
| 51 | esac |
| 52 | done |
| 53 | |
| 54 | versions=("$@") |
| 55 | |
| 56 | if [ "$versions" = "--unset" ]; then |
| 57 | rm -f .python-version |
| 58 | elif [ -n "$versions" ]; then |
| 59 | pyenv-version-file-write ${FORCE:+-f }.python-version "${versions[@]}" |
| 60 | else |
| 61 | if version_file="$(pyenv-version-file "$PWD")"; then |
| 62 | IFS=: versions=($(pyenv-version-file-read "$version_file")) |
| 63 | for version in "${versions[@]}"; do |
| 64 | echo "$version" |
| 65 | done |
| 66 | else |
| 67 | echo "pyenv: no local version configured for this directory" >&2 |
| 68 | exit 1 |
| 69 | fi |
| 70 | fi |