Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 641421
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T21:03:39+00:00 2026-05-13T21:03:39+00:00

I am writing a script and in my script I have this function: def

  • 0

I am writing a script and in my script I have this function:

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

This worked fine until I added the try catches. I did have the if, elif, the function ran just fine. Here is my call and the peice of code that runs right before it:

   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()

My error is:

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

I don’t know what line 47 means because the call is on line 2101, again before I added the trys, it found the function just fine. I also switched the first commit to be before the insert_image call when I added the trys like you see now, before the commit was after we called insert_image. I checked indents, and spaces, and tabs w/ no avail.

I use TextMate, when I run the script from TextMate, I get a syntax error here:

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

It points to the ( on (folder… But I don’t see where I have a syntax error. Please help. I have been working on this script for a couple of weeks now, this was supposed to be the last run to test and call it finished 🙁

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T21:03:40+00:00Added an answer on May 13, 2026 at 9:03 pm

    You have syntax errors in your function:

    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
    

    It should be:

    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
    

    You also have some broad catches, that catch those SyntaxErrors and hide errors, but insert_image is not defined this way. Never use alone except:, always put name of the Exception, that you want to catch. Otherwise you will also catch things like SyntaxError and this is very dangerous.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote this little function for writing out HTML tags: def html_tag(tag, content=None, close=True,
I'm writing a bash script and I have errexit set, so that the script
I'm writing a bat script and I have to save the date from a
I'm writing a script that will ping my ip range. Here's what I have
I am writing a script for renaming/copying the content of a file. I have
Folks, I have a problem. I am a writing a script in python which
I am writing a shell script in which I have to find the last
I am writing some BASH script and I want it have some error handling
I have a script and it's display show's upload progress by writing to the
I am writing a menu driven shell script and I have yes / no

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.