Search code examples
python-3.xflaskgoogle-cloud-platformgcloudtpu

Gcloud not found in flask api


I am using the following python method in GCP. This methods is in test.py


@functools.lru_cache()
def get_project():
    return subprocess.check_output("gcloud config list --format 'value(core.project)'", shell=True).decode(
        "utf-8").strip()

When I run test.py solely on tpu, it works and when I use this method in flask API then I get error

'gcloud not found'.

However, the same method works solely and under flask API in GCP VM.

I am not able to figure out what could be the possible cause of this.


Solution

  • This is not exactly an answer to your question, but you might be interested in knowing about the metadata server.

    From this answer we can more or less deduce that the metadata server also works with TPUs. Note that I'm not 100% sure on this though.

    Try the following code to see if you can get the project id with it.

    import requests
    
    
    def get_project():
        # https://cloud.google.com/compute/docs/metadata/default-metadata-values#project_metadata
        response = requests.get(
            "http://metadata.google.internal/computeMetadata/v1/project/project-id",
            headers={"Metadata-Flavor": "Google"}
        )
        return response.text