如何在MACOS中的python3.6中正确导入google云库?

如何在MACOS中的python3.6中正确导入google云库?

我正在运行一个使用谷歌云库的 python3.6 项目:

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')

但是,我找不到正确导入 Google 云库的方法,pip install 我试过了venv/bin/pip install google-api-python-clientpip install -U google-cloud-secret-manager控制台消息显示安装成功。但是,当我运行上述代码时,出现此错误:

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

我不知道为什么连正确安装库都这么困难。这应该是一个非常简单和顺利的过程。我在这里遗漏了什么?

答案1

在这里回答我自己:

添加以下行到requirements.txt

google-cloud-secret-manager==2.0.0

然后安装pip install -r requirements.txt

起初不知怎么的,这个方法不管用。经过反复尝试,终于成功了。

答案2

您可以按照以下步骤操作,或者官方文件为了设置 Python 客户端库:

您还可以按照下一个 CodeLabs 进行设置Secret Manager 的 Python 客户端库

另请查看Secret Manager API 的 Python 客户端

希望对您有所帮助。如果您在执行上述步骤时遇到任何问题,请告诉我。

相关内容