gcloud命令说`pyenv:python2:找不到命令,即使python2在终端中也是有效的

问题描述 投票:5回答:1

我正在使用macOS Mojave 10.14.2和pyenv 1.2.7。

这些是我的pyenv配置。python2似乎可以在终端中工作。

$ pyenv versions
  system
* 2.7.14
  3.6.4

$ pyenv global
2.7.14
3.6.4

$ pyenv local
2.7.14

$ python2
Python 2.7.14 (default, Sep 12 2018, 16:35:37)
[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.39.2)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> (It works!)

但是当我使用gcloud命令时,它无法说找不到python2。

$ gcloud config list
pyenv: python2: command not found

The `python2' command exists in these Python versions:
  2.7.14

另一方面,也需要python2的npm命令也可以正常工作。

暂时由pyenv shell命令修复,但是仅在关闭端子之前才有效。(此处引用解决方案:https://github.com/pyenv/pyenv/issues/1159

$ pyenv shell 2.7.14 3.6.4
$ gcloud config list -> Works!

如何永久解决此问题?

macos python-2.7 google-cloud-platform gcloud
1个回答
0
投票

我最近遇到此错误,并找到了解决方案。我正在将pyenv与虚拟环境(pyenv-viritualenv)一起使用,并且试图从python 3虚拟环境中运行gcloud

问题是,当您从virtualenv中的某个位置运行gcloud时,它仅知道.python-version文件中为该目录指定的python版本。因此,除了python 3版本外,您还必须指定要运行的python 2版本:

pyenv local <virtualenv name> <python 2 version>

除了完成其他相关问题的答案中提到的CLOUDSDK_PYTHON位之外,我还这样做了:

export CLOUDSDK_PYTHON=$(which python2)

我将此添加到了.bash_profile

© www.soinside.com 2019 - 2024. All rights reserved.