首页 > 解决方案 > kubectl pyenv python 版本冲突

问题描述

我正在尝试在我的 mac 上运行一些 kubectl 命令,我使用 pyenv 来管理 python 版本。每当我运行某些 kubectl 命令时,都会导致 kubectl 指出它找不到 python2 命令的错误。

josh@venus:~/pjx/distribut_io ❯ kubectl get pods
Unable to connect to the server: error executing access token command "/Users/josh/google-cloud-sdk/bin/gcloud config config-helper --format=json": err=exit status 127 output= stderr=pyenv: python2: command not found

The `python2' command exists in these Python versions:
  2.7.4
  fp


josh@venus:~/pjx/distribut_io ❯ which python
/Users/josh/.pyenv/shims/python
josh@venus:~/pjx/distribut_io ❯ which python2
/Users/josh/.pyenv/shims/python2

我尝试更改.python-version为 2.7.4、system 和其他几个版本,但我似乎无法让它工作。我很想拉线,有人可以提供一些方向吗?

标签: pythonshellkubectlpyenv

解决方案


kubectl 不需要 Python,但 gcloud 需要。

这是 gcloud 源代码的一部分(实际上是用 bash 编写的):

#  CLOUDSDK_ROOT_DIR            (a)  installation root dir
#  CLOUDSDK_PYTHON              (u)  python interpreter path
#  CLOUDSDK_PYTHON_ARGS         (u)  python interpreter arguments
#  CLOUDSDK_PYTHON_SITEPACKAGES (u)  use python site packages

...

# Cloud SDK requires python 2.7
case $CLOUDSDK_PYTHON in
*python2*)
  ;;
*python[0-9]*)
  CLOUDSDK_PYTHON=
  ;;
esac
# if CLOUDSDK_PYTHON is empty
if [ -z "$CLOUDSDK_PYTHON" ]; then
  # if python2 exists then plain python may point to a version != 2
  if which python2 >/dev/null; then
    CLOUDSDK_PYTHON=python2
  elif which python2.7 >/dev/null; then
    # this is what some OS X versions call their built-in Python
    CLOUDSDK_PYTHON=python2.7
  else
    CLOUDSDK_PYTHON=python
  fi
fi

因此,请检查您的环境变量以了解启动 gcloud 时发生的情况。

作为一个简单的解决方法,您可以使用自制软件安装 python2,或者只创建一个符号链接 python2 -> python:

sudo ln -s `which python` $(dirname `which python`)/python2

另一种方法是按照pyenv 文档中的描述配置 pyenv 设置,以获取必要的 python 版本。


推荐阅读