UnicodeDecodeError:'ascii'编解码器无法解码位置9中的字节0xc2:序号不在范围内(128)

问题描述:

我没有编写python。我第一次看到这样的代码。 我的Python gsutil文件:UnicodeDecodeError:'ascii'编解码器无法解码位置9中的字节0xc2:序号不在范围内(128)

#!/usr/bin/env python 
# 
# Copyright 2013 Google Inc. All Rights Reserved. 
# 

"""A convenience wrapper for starting gsutil.""" 

import os 
import sys 


import bootstrapping 
from googlecloudsdk.core import config 
from googlecloudsdk.core import metrics 
from googlecloudsdk.core import properties 
from googlecloudsdk.core.credentials import gce as c_gce 


def _MaybeAddBotoOption(args, section, name, value): 
    if value is None: 
    return 
    args.append('-o') 
    args.append('{section}:{name}={value}'.format(
     section=section, name=name, value=value)) 


def main(): 
    """Launches gsutil.""" 

    project, account = bootstrapping.GetActiveProjectAndAccount() 
    pass_credentials = (
     properties.VALUES.core.pass_credentials_to_gsutil.GetBool() and 
     not properties.VALUES.auth.disable_credentials.GetBool()) 

    if pass_credentials and account not in c_gce.Metadata().Accounts(): 
    gsutil_path = config.Paths().LegacyCredentialsGSUtilPath(account) 

    # Allow gsutil to only check for the '1' string value, as is done 
    # with regard to the 'CLOUDSDK_WRAPPER' environment variable. 
    os.environ['CLOUDSDK_CORE_PASS_CREDENTIALS_TO_GSUTIL'] = '1' 

    boto_config = os.environ.get('BOTO_CONFIG', '') 
    boto_path = os.environ.get('BOTO_PATH', '') 

    # We construct a BOTO_PATH that tacks the refresh token config 
    # on the end. 
    if boto_config: 
     boto_path = os.pathsep.join([boto_config, gsutil_path]) 
    elif boto_path: 
     boto_path = os.pathsep.join([boto_path, gsutil_path]) 
    else: 
     path_parts = ['/etc/boto.cfg', 
        os.path.expanduser(os.path.join('~', '.boto')), 
        gsutil_path] 
     boto_path = os.pathsep.join(path_parts) 

    if 'BOTO_CONFIG' in os.environ: 
     del os.environ['BOTO_CONFIG'] 
    os.environ['BOTO_PATH'] = boto_path 

    # Tell gsutil whether gcloud analytics collection is enabled. 
    os.environ['GA_CID'] = metrics.GetCIDIfMetricsEnabled() 

    args = [] 

    _MaybeAddBotoOption(args, 'GSUtil', 'default_project_id', project) 
    if pass_credentials and account in c_gce.Metadata().Accounts(): 
    # Tell gsutil to look for GCE service accounts. 
    _MaybeAddBotoOption(args, 'GoogleCompute', 'service_account', 'default') 

    proxy_params = properties.VALUES.proxy 
    proxy_address = proxy_params.address.Get() 
    if proxy_address: 
    _MaybeAddBotoOption(args, 'Boto', 'proxy', proxy_address) 
    _MaybeAddBotoOption(args, 'Boto', 'proxy_port', proxy_params.port.Get()) 
    _MaybeAddBotoOption(args, 'Boto', 'proxy_rdns', proxy_params.rdns.GetBool()) 
    _MaybeAddBotoOption(args, 'Boto', 'proxy_user', proxy_params.username.Get()) 
    _MaybeAddBotoOption(args, 'Boto', 'proxy_pass', proxy_params.password.Get()) 
    disable_ssl = properties.VALUES.auth.disable_ssl_validation.GetBool() 
    _MaybeAddBotoOption(args, 'Boto', 'https_validate_certificates', 
         None if disable_ssl is None else not disable_ssl) 
    _MaybeAddBotoOption(args, 'Boto', 'ca_certificates_file', 
         properties.VALUES.core.custom_ca_certs_file.Get()) 

    bootstrapping.ExecutePythonTool('platform/gsutil', 'gsutil', *args) 


if __name__ == '__main__': 
    version = bootstrapping.GetFileContents('platform/gsutil', 'VERSION') 
    bootstrapping.CommandStart('gsutil', version=version) 

    blacklist = { 
     'update': 'To update, run: gcloud components update', 
    } 

    bootstrapping.CheckForBlacklistedCommand(sys.argv, blacklist, warn=True, 
              die=True) 
    # Don't call bootstrapping.PreRunChecks because anonymous access is 
    # supported for some endpoints. gsutil will output the appropriate 
    # error message upon receiving an authentication error. 
    bootstrapping.CheckUpdates('gsutil') 
    main() 

当我从命令行运行它:

gsutil config 

它给我这个错误:

Traceback (most recent call last): 
    File "D:\Programs\Google\Cloud SDK\google-cloud-sdk\bin\..\bin\bootstrapping\g 
sutil.py", line 102, in <module> 
    main() 
    File "D:\Programs\Google\Cloud SDK\google-cloud-sdk\bin\..\bin\bootstrapping\g 
sutil.py", line 55, in main 
    boto_path = os.pathsep.join(path_parts) 
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc2 in position 9: ordinal 
not in range(128) 

如何解决呢?发生错误的行是:

boto_path = os.pathsep.join(path_parts) 
+0

与此问题相关的一些评论张贴在[此线程](https://*.com/questions/46471114/)上。 – Kamran

我想你正在使用python3运行此代码段,但这仅适用于python2。

我想你已经知道有两个不兼容的python版本吧?

+0

我安装了google云sdk。并尝试在那里创建桶。我为此使用'gsutil'工具。但是当我打电话给它时,它给我带来了错误。 –

+0

@JaySmith在你的终端中,运行命令'python --version'。如果它给你一些类似“Python 3.5.2”的东西,那么你正在使用python3。你需要python2来运行'gsutil'工具。 – Sraw