[Python] Can I check if a script is running inside a Compute Engine or in a local environment?

Discussão em 'Python' iniciado por Stack, Março 13, 2025.

  1. Stack

    Stack Membro Participativo

    I just wanted to know if there is a way to check whether a Python script is running inside a Compute Engine or in a local environment?

    I want to check that in order to know how to authenticate, for example when a script runs on a Compute Engine and I want to initiate a BigQuery client I do not need to authenticate but when it comes to running a script locally I need to authenticate using a service account JSON file.

    If I knew whether a script is running locally or in a Compute Engine I would be able to initiate Google services accordingly.

    I could put initialization into a try-except statement but maybe there is another way?

    Any help is appreciated.

    Continue reading...

Compartilhe esta Página