Search code examples
pythonexception

Script raises HTTPError, but cannot catch


I have an error which I have a little difficulty understanding. I have a script which uses biopython to query a database. Sometimes, biopython can't find what we're looking for, and an HTTPError is thrown. I cannot, however catch the HTTPError, as I get the following error message:

HTTPError: HTTP Error 404: Not Found

During handling of the above exception, another exception occurred:

NameError Traceback (most recent call last) in () 51 UniProt = text[index+9:index+15] 52 uniprot_IDs[bigg_ID] = UniProt ---> 53 except HTTPError: 54 if err.code == '404': 55 uniprot_IDs[biGG_ID] = None

NameError: name 'HTTPError' is not defined

How can an error which is not defined be thrown in the first place? What am I missing?

This is the relevant code:

from Bio.KEGG import REST, Enzyme
from DataTreatment import openJson, write

...

try:
    ec_number = some_string
    text = REST.kegg_get('ec:'+ec_number).read()

...

except HTTPError:
    if err.code == '404':
        a_dict[a_key] = None

Solution

  • You need to import the HTTPError-class, try this;

    In the top of your code, add

    from urllib.error import HTTPError
    

    Source: Entrez._HTTPError vs. Entrez.HTTPError (via Entrez.efetch)