我的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模块上的文件属性并且它是相同的)没有引发错误。
答案 0 :(得分:0)
由于某些原因,在通过pycharm运行unittes时,Mac上没有设置正确的环境变量。添加以下代码(来自this answer)为我解决了这个问题:
import os
os.environ['LC_ALL'] = 'en_US.UTF-8'
os.environ['LANG'] = 'en_US.UTF-8'