python定义了函数却显示未定义_python

在Python脚本中遇到一个错误,尝试从URL下载图片并保存。原本的函数`insert_image`调用出现问题,导致找不到该函数。错误发生在尝试使用`urllib`模块打开和下载图片的代码块中。在添加异常处理后,脚本无法识别`insert_image`函数。检查了缩进和空格,但问题依然存在。现在需要解决这个函数调用的错误,以便脚本能正确处理图片的插入和提交操作。
摘要由CSDN通过智能技术生成

我正在编写一个脚本,在我的脚本中,我具有以下功能:

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:///%s/%sPR-IT,PM.jpg" % (folder, sku)):

urllib.URLopener().retrieve("http:///%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:///%s/%sPK-PT,PM.jpg" % (folder, sku)):

urllib.URLopener().retrieve("http:///%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:///%s/%sCC-PT,IM.jpg" % (folder, sku)):

urllib.URLopener().retrieve("http:///%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

在我添加尝试捕获之前,此方法运行良好。 我确实有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 "", line 47, in

NameError: name 'insert_image' is not defined

我不知道第47行是什么意思,因为该调用位于第2101行,在我添加尝试之前,它再次发现该函数很好。 当我像现在看到的那样添加尝试时,我还将第一个提交切换到insert_image调用之前,而在我们调用insert_image之后则提​​交之前。 我检查了缩进,空格和制表符w /无济于事。

我使用TextMate,当我从TextMate运行脚本时,在这里出现语法错误:

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

它指向(上的(文件夹。。。但是我看不到语法错误。请帮忙。我已经在这个脚本上工作了几周了,这应该是最后一次运行测试并称其为:(

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值