TransWikia.com

How to properly import google cloud libraries in python3.6 in MACOS?

Server Fault Asked by Kid_Learning_C on November 29, 2020

I’m running a python3.6 project that uses the google cloud libraries:

from google.cloud import secretmanager

def access_secret_version(project_id, secret_id, version_id=1):
    """
    Access the payload for the given secret version if one exists. The version
    can be a version number as a string (e.g. "5") or an alias (e.g. "latest").
    """
    # Create the Secret Manager client.
    client = secretmanager.SecretManagerServiceClient()

    # Build the resource name of the secret version.
    name = f"projects/{project_id}/secrets/{secret_id}/versions/{version_id}"

    # Access the secret version.
    response = client.access_secret_version(request={"name": name})

    # Print the secret payload.
    #
    # WARNING: Do not print the secret in a production environment - this
    # snippet is showing how to access the secret material.
    payload = response.payload.data.decode("UTF-8")
    print("Plaintext: {}".format(payload))
    print(response)


if __name__ == "__main__":

    access_secret_version('my-project', 'APAC_URL')
    access_secret_version('my-project', 'APAC_SECRET')
    access_secret_version('my-project', 'PASSWORD')

However, I couldn’t find a way to properly import the google cloud libraries with pip install
I haved tried venv/bin/pip install google-api-python-client and pip install -U google-cloud-secret-manager, and the console messages show that the installation was successful. However when I run the above code, I get this error:

Traceback (most recent call last):
  File "utils/secret_accessor.py", line 1, in <module>
    from google.cloud import secretmanager
  File "/Users/my-code-base/automated_test/venv/lib/python3.6/site-packages/google/cloud/secretmanager/__init__.py", line 18, in <module>
    from google.cloud.secretmanager_v1.services.secret_manager_service.async_client import (
  File "/Users/my-code-base/automated_test/venv/lib/python3.6/site-packages/google/cloud/secretmanager_v1/__init__.py", line 18, in <module>
    from .services.secret_manager_service import SecretManagerServiceClient
  File "/Users/my-code-base/automated_test/venv/lib/python3.6/site-packages/google/cloud/secretmanager_v1/services/secret_manager_service/__init__.py", line 18, in <module>
    from .client import SecretManagerServiceClient
  File "/Users/my-code-base/automated_test/venv/lib/python3.6/site-packages/google/cloud/secretmanager_v1/services/secret_manager_service/client.py", line 27, in <module>
    from google.api_core import gapic_v1  # type: ignore
  File "/Users/my-code-base/automated_test/venv/lib/python3.6/site-packages/google/api_core/gapic_v1/__init__.py", line 18, in <module>
    from google.api_core.gapic_v1 import config
  File "/Users/my-code-base/automated_test/venv/lib/python3.6/site-packages/google/api_core/gapic_v1/config.py", line 23, in <module>
    import grpc
  File "/Users/my-code-base/automated_test/venv/lib/python3.6/site-packages/grpc/__init__.py", line 23, in <module>
    from grpc._cython import cygrpc as _cygrpc
ImportError: dlopen(/Users/my-code-base/automated_test/venv/lib/python3.6/site-packages/grpc/_cython/cygrpc.cpython-36m-darwin.so, 2): Symbol not found: __PyErr_FormatFromCause
  Referenced from: /Users/my-code-base/automated_test/venv/lib/python3.6/site-packages/grpc/_cython/cygrpc.cpython-36m-darwin.so
  Expected in: flat namespace
 in /Users/my-code-base/automated_test/venv/lib/python3.6/site-packages/grpc/_cython/cygrpc.cpython-36m-darwin.so

I don’t know why it is so difficult to even get the libraries installed properly. It should be a very simple and smooth process. What am I missing here?

2 Answers

Answering myself here:

add the below line to requirements.txt:

google-cloud-secret-manager==2.0.0

Then install with pip install -r requirements.txt

Somehow this did not work at first. After some trial-and-error, it finally worked.

Answered by Kid_Learning_C on November 29, 2020

You can follow the next steps or the Official Documentation in order to set up Python Client Library:

You can also follow the next CodeLabs to set up Python Client Library for Secret Manager

Also please have a look into the Python Client for Secret Manager API

I hope it helps. Let me know if you have any issues when performing the previous steps.

Answered by Nibrass H on November 29, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP