Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/97.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 函数未定义,但实际上已定义_Python_Nameerror - Fatal编程技术网

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

Python 函数未定义,但实际上已定义,python,nameerror,Python,Nameerror,我正在编写脚本,在脚本中我有以下功能: 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 redund

我正在编写脚本,在脚本中我有以下功能:

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 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
我的错误是:

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
16:33:46153调试[pylons admin]插入说明
16:33:46164调试[pylons admin]插入的属性
16:33:46164调试[挂架管理]插入1
回溯(最近一次呼叫最后一次):
文件“”,第47行,在
名称错误:未定义名称“插入图像”
我不知道第47行是什么意思,因为调用是在第2101行,在我添加trys之前,它发现函数很好。我还将第一次提交切换到了insert_image调用之前,当时我添加了try,正如您现在看到的,在提交之前是在我们调用insert_image之后。我检查了缩进、空格和制表符,但没有结果

我使用TextMate,当我从TextMate运行脚本时,我在这里得到一个语法错误:

 try urllib.urlopen("http://<path to images>/%s/%sPR-IT,PM.jpg" % (folder, sku)):
尝试urllib.urlopen(“http:///%s/%sPR-IT,PM.jpg”%(文件夹,sku)):

它指向(on)(文件夹…但我看不出哪里有语法错误。请提供帮助。我已经编写此脚本两周了,这应该是最后一次运行测试并将其称为已完成:(

您的函数中有语法错误:

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 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
尝试urllib.urlopen(“http:///%s/%sPR-IT,PM.jpg”%(文件夹,sku)):
urllib.URLopener().retrieve(“http:///%s/%sPR-IT,PM.jpg”%(文件夹,sku),%sPR-IT,PM.jpg”%(sku))
找到\u url=True
除:
找到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
试试看:
urllib.urlopen(“http:///%s/%sPR-IT,PM.jpg”%(文件夹,sku)):
urllib.URLopener().retrieve(“http:///%s/%sPR-IT,PM.jpg”%(文件夹,sku),%sPR-IT,PM.jpg”%(sku))
找到\u url=True
除:
找到url=False

您还有一些广泛的捕获,可以捕获那些语法错误并隐藏错误,但是插入图像不是这样定义的。永远不要单独使用
,除非:
,始终输入要捕获的异常的名称。否则您还将捕获像SyntaxError这样的内容,这是非常危险的。

您的函数中有语法错误:

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 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
尝试urllib.urlopen(“http:///%s/%sPR-IT,PM.jpg”%(文件夹,sku)):
urllib.URLopener().retrieve(“http:///%s/%sPR-IT,PM.jpg”%(文件夹,sku),%sPR-IT,PM.jpg”%(sku))
找到\u url=True
除:
找到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
试试看:
urllib.urlopen(“http:///%s/%sPR-IT,PM.jpg”%(文件夹,sku)):
urllib.URLopener().retrieve(“http:///%s/%sPR-IT,PM.jpg”%(文件夹,sku),%sPR-IT,PM.jpg”%(sku))
找到\u url=True
除:
找到url=False

你也有一些广泛的捕获,可以捕获那些语法错误并隐藏错误,但是insert_image不是这样定义的。永远不要单独使用
,除非:
,总是输入你想要捕获的异常的名称。否则你也会捕获像SyntaxError这样的东西,这是非常危险的。

这听起来像是一个空白方法之前的问题。不正确的空格已将其移动到正常代码路径之外,如果它在类中,则可能不再出现在类中。这听起来像是在方法之前出现了空格问题。不正确的空格已将其移动到正常代码路径之外,如果它在类中,则可能不再出现我知道这很明显。谢谢。我来自PHP,以前从未真正使用过trys。我怎么知道我的异常会是什么呢?我知道会是url找不到,但我怎么知道除了什么之外应该加什么呢?公平地说,在运行时你几乎不会遇到
语法错误;它们是生成的d在编译时,在执行
try
块之前很久。啊,这就是它找不到函数的原因,因为当它编译时,它发现了错误,然后排序排除了它?ooooook。这可能会发生。只要遵守规则,except永远不应该为空,您就会没事:-)@我将KacieHouser用作默认值
,但例外情况为e:print str(e)
告诉我失败的原因,然后在上面添加一个额外的异常来处理唯一的异常。你可以想象,除非你阅读所有使用的模块的完整文档,否则你无法知道会出现哪个异常,所以在编码时打印异常是一种方便的方法。我知道这是显而易见的。谢谢你。我来自PHP,从来没有读过r以前我经常使用trys。我怎么知道我的异常会是什么?我知道它会是url not found,但我怎么知道在except上加什么呢?公平地说,在运行时你几乎不会遇到
SyntaxError
;它们是在编译时生成的,远远早于
try
块被执行之前。啊,这就是为什么它不能执行的原因查找函数,因为当它编译它时,它发现了错误,只是排序排除了它?Oooook。这可能会发生。只要遵守规则,except永远不应该为空,您就可以了:-)@KacieHouser我使用默认值
Exception作为e:print str(e)
为了让我知道失败的原因,请在上面添加一个额外的异常来处理唯一的异常。正如你所能想象的,除非你阅读所使用的每个模块的完整文档,否则你无法知道会出现哪个异常,所以在编码时打印异常是一种方便的方法。-1:哇,代码太多了。你能把它减少到刚好足够的c吗ode来显示问题?-1:哇,代码太多了。你能把代码缩减到刚好足够显示问题的代码吗?