函数未定义但实际上已定义

0 投票
2 回答
2822 浏览
提问于 2025-04-15 19:57

我正在写一个脚本,在这个脚本里有一个函数:

def insert_image(cursor, object_id, sku):
    product_obj = core.Object.get(object_id)
    string_sku = str(sku)
    folder = string_sku[0] + string_sku[1] + string_sku[2]
    found_url = False
    # KLUDGE This is ugly and redundant, however putting this in an if elif elif else throws error when url not found
    # try this url first
    try urllib.urlopen("http://<path to images>/%s/%sPR-IT,PM.jpg" % (folder, sku)):
        urllib.URLopener().retrieve("http://<path to images>/%s/%sPR-IT,PM.jpg" % (folder, sku), "%sPR-IT,PM.jpg" % (sku))
        found_url = True
    except:
        found_url = False
    # If that one didn't work try this one
    if found_url == False:
        try urllib.urlopen("http://<path to images>/%s/%sPK-PT,PM.jpg" % (folder, sku)):
            urllib.URLopener().retrieve("http://<path to images>/%s/%sPK-PT,PM.jpg" % (folder, sku), "%sPK-PT,PM.jpg" % (sku))
            found_url = True
        except:
            found_url = False
    # If still nothing, one last attempt
    if found_url == False:
        try urllib.urlopen("http://<path to images>/%s/%sCC-PT,IM.jpg" % (folder, sku)):
            urllib.URLopener().retrieve("http://<path to images>/%s/%sCC-PT,IM.jpg" % (folder, sku), "%sCC-PT,IM.jpg" % (sku))
            found_url = True
        except:
            found_url = False
    # We failed to find an image for this product, it will have to be done manually
    if found_url == False:
        log.info("Could not find the image on notions")
        return False

    # Hey we found something! Open the image....
    send_image = open('%sPK-PT,PM.jpg' % sku, 'r')
    # ...and send it for processing
    if product_obj.set_image(send_image, 5, 1) == False:
        return False
    else:
        log.debug("Inserted Image")
        return True

这个函数运行得很好,直到我添加了错误处理的部分(try catches)。之前我有用if和elif,函数运行得也很顺利。这里是我调用这个函数的代码,以及在调用之前的那段代码:

   if rollback == False:
        # Nah -- it's all good SAVE IT!
        count += 1
        log.debug("INSERT %s" % count)
        conn.commit()
    else:
        # Yeah something went wrong, errors reported why, roll it back
        conn.rollback()
        log.debug("skipped %s" % skip_count)

  # Insert images
        if rollback == False:
            sku = row[0]
            if insert_image(cursor, object_id, sku) == False:
                log.error("Could not get the image inserted for product: %s" % object_id)
                conn.rollback()
            else:
                conn.commit()

我遇到的错误是:

16:33:46,153 DEBUG [pylons-admin] Inserted Description
16:33:46,164 DEBUG [pylons-admin] Inserted Attributes
16:33:46,164 DEBUG [pylons-admin] INSERT 1
Traceback (most recent call last):
File "<console>", line 47, in <module>
NameError: name 'insert_image' is not defined

我不明白第47行是什么意思,因为调用是在第2101行。之前我添加错误处理之前,函数运行得很好。我还把第一个提交(commit)放在了insert_image调用之前,像你现在看到的那样,之前提交是在调用insert_image之后。我检查了缩进、空格和制表符,但都没有找到问题。

我使用的是TextMate,当我从TextMate运行这个脚本时,这里出现了一个语法错误:

 try urllib.urlopen("http://<path to images>/%s/%sPR-IT,PM.jpg" % (folder, sku)):

错误指向了(folder...的那个括号,但我看不出哪里有语法错误。请帮帮我。我已经在这个脚本上工作了几周,这本来是最后一次测试,想要结束这个项目的。 :(

2 个回答

0

这听起来像是你在方法前面有空格问题。错误的空格把它移出了正常的代码流程,如果它在一个类里面,可能看起来就不再属于这个类了。

3

你的函数里有语法错误:

try urllib.urlopen("http://<path to images>/%s/%sPR-IT,PM.jpg" % (folder, sku)):
        urllib.URLopener().retrieve("http://<path to images>/%s/%sPR-IT,PM.jpg" % (folder, sku), "%sPR-IT,PM.jpg" % (sku))
        found_url = True
    except:
        found_url = False

应该是:

try:
    urllib.urlopen("http://<path to images>/%s/%sPR-IT,PM.jpg" % (folder, sku)):
    urllib.URLopener().retrieve("http://<path to images>/%s/%sPR-IT,PM.jpg" % (folder, sku), "%sPR-IT,PM.jpg" % (sku))
    found_url = True
except:
    found_url = False

你还有一些很宽泛的错误捕捉,这样会把语法错误也捕捉到,从而隐藏了真正的错误。不过,insert_image 这样是无法定义的。不要单独使用 except:,一定要写出你想捕捉的错误类型。否则,你也会捕捉到像语法错误这样的东西,这样是非常危险的。

撰写回答