Question: Biopython: Entrez.efetch causes UnboundLocalError
gravatar for jens.einloft
5.9 years ago by
jens.einloft0 wrote:



i work with Biopyhton and try to find pathways in which certain Proteins are involved. Therefore i use the following code:

from Bio import Entrez
handle = Entrez.efetch(id = "1134002", db = "biosystems", retmode = "xml")
data =    

That will cause the following error:

  File "/home/jens/Desktop/", line 19, in <module>
    data =    
  File "/usr/lib/python2.7/dist-packages/Bio/Entrez/", line 372, in read
    record =
  File "/usr/lib/python2.7/dist-packages/Bio/Entrez/", line 187, in read
  File "/usr/lib/python2.7/dist-packages/Bio/Entrez/", line 486, in externalEntityRefHandler
UnboundLocalError: local variable 'url' referenced before assignment


I take a look into the class and found this:

def externalEntityRefHandler(self, context, base, systemId, publicId):
    """The purpose of this function is to load the DTD locally, instead
    of downloading it from the URL specified in the XML. Using the local
    DTD results in much faster parsing. If the DTD is not found locally,
    we try to download it. If new DTDs become available from NCBI,
    putting them in Bio/Entrez/DTDs will allow the parser to see them."""
    urlinfo = _urlparse(systemId)
    #Following attribute requires Python 2.5+
    #if urlinfo.scheme=='http':
    if urlinfo[0]=='http':
        # Then this is an absolute path to the DTD.
        url = systemId
    elif urlinfo[0]=='':
        # Then this is a relative path to the DTD.
        # Look at the parent URL to find the full path.
            url = self.dtd_urls[-1]
        except IndexError:
            # Assume the default URL for DTDs if the top parent
            # does not contain an absolute path
            source = ""
            source = os.path.dirname(url)
        # urls always have a forward slash, don't use os.path.join
        url = source.rstrip("/") + "/" + systemId

I have done a little bit debugging and found the error. In my case, the urlinfo[0] contains "ftp". This case is not handled in the if/elif construct. And so the url parameter is not set.

Is this a bug in Biopython or do i handle it the wrong way?

biopython python • 3.1k views
ADD COMMENTlink modified 5.8 years ago by Peter5.9k • written 5.9 years ago by jens.einloft0
gravatar for Peter
5.8 years ago by
Scotland, UK
Peter5.9k wrote:

It's a bug, reported about the same time as your question here:

ADD COMMENTlink written 5.8 years ago by Peter5.9k
Please log in to add an answer.


Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 1897 users visited in the last hour