【问题标题】:Python IO module unknown encoding while unittesting GAE单元测试GAE时Python IO模块未知编码
【发布时间】:2018-01-06 08:16:23
【问题描述】:

我的 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 属性,它是相同的)没有引发错误。

【问题讨论】:

    标签: python google-app-engine google-cloud-storage pytest python-unittest


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-08-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-19
      相关资源
      最近更新 更多