Question: Biopython TimeExceptionError how to handle?
gravatar for Xylanaser
2.6 years ago by
Poland - Warsaw - SGGW/WULS
Xylanaser60 wrote:

Hey how can i handle TimeExceptionError? Im downloading abstracts and sometimes it occurs. Cheers,X

edit: to be clear sometimes after 100 abstracts, sometimes after 10000

Traceback (most recent call last):

  File "<ipython-input-17-d84c4160e0e2>", line 1, in <module>
    runfile('C:/Users/m/Dropbox/', wdir='C:/Users/m/scripts')

  File "C:\Users\m\Anaconda3\lib\site-packages\spyder\utils\site\", line 880, in runfile
    execfile(filename, namespace)

  File "C:\Users\m\Anaconda3\lib\site-packages\spyder\utils\site\", line 102, in execfile
    exec(compile(, filename, 'exec'), namespace)

  File "C:/Users/m/Dropbox/", line 65, in <module>
    rettype ='medline', retmode = 'gb')

  File "C:\Users\m\Anaconda3\lib\site-packages\Bio\Entrez\", line 180, in efetch
    return _open(cgi, variables, post=post)

  File "C:\Users\m\Anaconda3\lib\site-packages\Bio\Entrez\", line 527, in _open
    handle = _urlopen(cgi)

  File "C:\Users\m\Anaconda3\lib\urllib\", line 223, in urlopen
    return, data, timeout)

  File "C:\Users\m\Anaconda3\lib\urllib\", line 526, in open
    response = self._open(req, data)

  File "C:\Users\m\Anaconda3\lib\urllib\", line 544, in _open
    '_open', req)

  File "C:\Users\m\Anaconda3\lib\urllib\", line 504, in _call_chain
    result = func(*args)

  File "C:\Users\m\Anaconda3\lib\urllib\", line 1361, in https_open
    context=self._context, check_hostname=self._check_hostname)

  File "C:\Users\m\Anaconda3\lib\urllib\", line 1321, in do_open
    r = h.getresponse()

  File "C:\Users\m\Anaconda3\lib\http\", line 1331, in getresponse

  File "C:\Users\m\Anaconda3\lib\http\", line 297, in begin
    version, status, reason = self._read_status()

  File "C:\Users\m\Anaconda3\lib\http\", line 258, in _read_status
    line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")

  File "C:\Users\m\Anaconda3\lib\", line 586, in readinto
    return self._sock.recv_into(b)

  File "C:\Users\m\Anaconda3\lib\", line 1002, in recv_into
    return, buffer)

  File "C:\Users\m\Anaconda3\lib\", line 865, in read
    return, buffer)

  File "C:\Users\m\Anaconda3\lib\", line 625, in read
    v =, buffer)

TimeoutError: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond

my code here:

abstracts_holder = []
counterAB = 0
my_list = open('my_list.txt', 'r')

for ID in my_list:

    handle = Entrez.efetch(db="pubmed", id=ID,
                           rettype ='medline', retmode = 'gb')
    for rec in medRecords:

        if rec.get('AB') != None :

ADD COMMENTlink modified 2.6 years ago • written 2.6 years ago by Xylanaser60

It might be useful to:

  • Show the code you are using
  • Show the exact Exception you get

Without that information, you are making this quite hard for us to guess.

Interesting guidelines for posting can be found in the following posts:

ADD REPLYlink modified 2.6 years ago • written 2.6 years ago by WouterDeCoster43k

Do you just want to catch the error so that it doesn't go un-handled or do you want to remedy the problem of not retrieving abstracts?

ADD REPLYlink written 2.6 years ago by Joe16k

I add some things , i want remedy the problem of not retrieving abstracts

ADD REPLYlink modified 2.6 years ago • written 2.6 years ago by Xylanaser60

I would suggest adding a time.sleep(5) in the loop, to avoid hammering the server with too many requests.

ADD REPLYlink written 2.6 years ago by WouterDeCoster43k

Would be my guess too. The server is probably kicking you off to avoid traffic overload.

If its not that, then somewhere along the pipeline you have an unstable network connection I would suppose, and I doubt there's a massive amount you could do about it if thats the case.

ADD REPLYlink written 2.6 years ago by Joe16k

Thanks for advice, i do the program that download sequences and give time.sleep(1) and its work well. Forgot about that :)

ADD REPLYlink written 2.6 years ago by Xylanaser60
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: 845 users visited in the last hour