Python 单元测试 GAE 时 IO 模块未知编码

Python IO module unknown encoding while unittesting GAE

我的 Python Google App Engine 应用程序有 written some unittests。以下是有问题的代码的提炼。

class TestCase(unittest.TestCase):

    def setUp(self):
        from google.appengine.ext import testbed
        self.testbed = testbed.Testbed()
        self.testbed.activate()
        self.testbed.init_urlfetch_stub()
        self.testbed.init_blobstore_stub()
        ndb.get_context().clear_cache()

    def tearDown(self):
        self.testbed.deactivate()

    def testing(self):

        from google.cloud import storage
        client = storage.Client()

我在使用 io 库打开文件(我的 gcloud 应用程序凭据)时收到编码 LookupError。

这是相关的堆栈跟踪和有问题的 io 代码(来自 pytest):

self = <test_1.TestCase testMethod=testing>

    def testing(self):
        from google.cloud import storage

>       client = storage.Client()

/Users/alex/projects/don/don_server/mobile/tests/test_1.py:66: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
/Users/alex/projects/don/don_server/lib/google/cloud/storage/client.py:59: in __init__
    _http=_http)
/Users/alex/projects/don/don_server/lib/google/cloud/client.py:223: in __init__
    _ClientProjectMixin.__init__(self, project=project)
/Users/alex/projects/don/don_server/lib/google/cloud/client.py:177: in __init__
    project = self._determine_default(project)
/Users/alex/projects/don/don_server/lib/google/cloud/client.py:190: in _determine_default
    return _determine_default_project(project)
/Users/alex/projects/don/don_server/lib/google/cloud/_helpers.py:181: in _determine_default_project
    _, project = google.auth.default()
/Users/alex/projects/don/don_server/lib/google/auth/_default.py:277: in default
    credentials, project_id = checker()
/Users/alex/projects/don/don_server/lib/google/auth/_default.py:117: in _get_gcloud_sdk_credentials
    credentials_filename)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

filename = '/Users/name/.config/gcloud/application_default_credentials.json'

    def _load_credentials_from_file(filename):
        """Loads credentials from a file.

        The credentials file must be a service account key or stored authorized
        user credentials.

        Args:
            filename (str): The full path to the credentials file.

        Returns:
            Tuple[google.auth.credentials.Credentials, Optional[str]]: Loaded
                credentials and the project ID. Authorized user credentials do not
                have the project ID information.

        Raises:
            google.auth.exceptions.DefaultCredentialsError: if the file is in the
                wrong format.
        """
>       with io.open(filename, 'r') as file_obj:
E       LookupError: unknown encoding:

当我在我的 GAE 本地开发服务器上 运行 这段代码时,我没有得到这个错误。此外,当我使用 shell 打开凭据文件时(我检查了 io 模块上的 file 属性并且它是相同的)没有引发错误。

由于某些原因,当 运行 单元测试通过 pycharm 时,未在 Mac 上设置正确的环境变量。添加以下代码(来自 this answer)为我解决了这个问题:

import os
os.environ['LC_ALL'] = 'en_US.UTF-8'
os.environ['LANG'] = 'en_US.UTF-8'