python定义了函数却显示未定义_python
我正在編寫一個腳本,在我的腳本中,我具有以下功能:
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)):
它指向(上的(文件夾。。。但是我看不到語法錯誤。請幫忙。我已經在這個腳本上工作了幾周了,這應該是最后一次運行測試并稱其為:(
總結
以上是生活随笔為你收集整理的python定义了函数却显示未定义_python的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: pcl画圆球_PCL之轨迹绘制(二)
- 下一篇: figure服务器无法显示,求大神帮帮忙