全部產品
Search
文件中心

Function Compute:當我運行Python語言的函數時,遇到報錯NoneType object has no attribute split怎麼辦?

更新時間:Aug 20, 2025

可能原因

入口函數定義錯誤,例如您在Python事件函數的入口函數中,建立了HTTP觸發器。

解決方案

請參考以下不同的函數類型定義您的入口函數:

  • Python事件函數的入口函數定義。詳細資料,請參見環境說明

    def handler(event, context):
            return 'hello world'
  • Python HTTP函數的入口函數定義。詳細資料,請參見請求處理常式(Handler)

    def handler(environ, start_response):
            context = environ['fc.context']
            # get request_body
            try:
                request_body_size = int(environ.get('CONTENT_LENGTH', 0))
            except (ValueError):
                request_body_size = 0
            request_body = environ['wsgi.input'].read(request_body_size)
            print('request_body: {}'.format(request_body))
            # do something here
    
            status = '200 OK'
            response_headers = [('Content-type', 'text/plain')]
            start_response(status, response_headers)
            # return value must be iterable
            return [b"Hello world!\n"]

可能原因

HTTP函數邏輯錯誤,例如忘記調用start_response參數,如下所示HTTP函數的範例程式碼中就忘記調用此參數。

def handler(environ, start_response):
        # do something here

        status = '200 OK'
        response_headers = [('Content-type', 'text/plain')]
        # forget to call start_response
        # start_response(status, response_headers)
        # return value must be iterable
        return [b"Hello world!\n"]

解決方案

請參考Python HTTP函數的函數入口及部署架構修改您的HTTP函數邏輯。詳細資料,請參見請求處理常式(Handler)

可能原因

在Python3的運行環境中,您將傳回值設定為不可迭代的Bytes了。

解決方案

請將傳回值設定為可迭代的Bytes,例如[json.dumps(result).encode()]

您可以參考以下樣本修改函數代碼:

import json
def handler(environ, start_response):
        # do something here
        result = {"code": "OK"}

        status = '200 OK'
        response_headers = [('Content-type', 'application/json')]
        start_response(status, response_headers)
        # return value must be iterable
        return  [json.dumps(result).encode()]