Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gcloud command says `pyenv: python2: command not found` even if python2 is valid in terminal

I'm using macOS Mojave 10.14.2, and pyenv 1.2.7 .

And these are my configs of pyenv. python2 seems to work in the terminal.

$ 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!)

But when I use gcloud command, it fails saying it cannot find python2.

$ gcloud config list
pyenv: python2: command not found

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

On the other hand, npm command, which requires python2 too, works fine.

It get fixed by pyenv shell command temporarily, but it is valid only until I close the terminal. (The solution is referred here: https://github.com/pyenv/pyenv/issues/1159)

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

How to fix this problem permanently ?

like image 343
Taichi Avatar asked Jan 22 '19 02:01

Taichi


People also ask

Why can't I find the right Python file in GCloud?

This is because the gcloud.batcommand can't find the right python.exe. I solved the problem by simply put SET CLOUDSDK_PYTHON=pathWherePythonexeLocate

Do I need Python for Google Cloud SDK?

Google Cloud SDK must have Python installed and on your PATH 0 GCloud powershell commands throw error but works fine in cmd Related 1441 How can I flush the output of the print function (unbuffer python output)? 3437 How to get the current time in Python 1344

Why can’t I install Python on Ubuntu?

It’s because the Python language is not installed as python but python3 or python2 (in some older Ubuntu versions). At some point in time in the distant past, Python was actually available as python package/executable. When Python released version 2, Ubuntu and other distros had to provide support for both Python version 1.x and 2.x.

Is there a Python command in Ubuntu?

However, if you try to use the python command in Ubuntu (and some other distributions), it will throw an error. If you pay attention to the error message, it clears a lot of things. The python command is actually python3 here. If you don’t understand it, no worries. I’ll explain things in detail here. Why there is no python command found on Ubuntu?


1 Answers

I was recently encountering this error and found a solution. I am using pyenv with virtual env (pyenv-viritualenv) and I was attempting to run gcloud from inside my python 3 virtual environment.

The issue is that when you run gcloud from a location within the virtualenv, it only knows of the python versions specified in the .python-version file for that directory. You therefore have to specify a python 2 version to run in addition to your python 3 version:

pyenv local <virtualenv name> <python 2 version>

I did this in addition to doing the CLOUDSDK_PYTHON bit mentioned in answers on other related questions:

export CLOUDSDK_PYTHON=$(which python2)

I added this to my .bash_profile

like image 165
rgutierrez1014 Avatar answered Sep 19 '22 11:09

rgutierrez1014