29

I am using Python 2.7 64 bit on Windows 8. I have Requests version 2.3 installed. I am trying to run this import statement as part of bringing in number of retries within my code:

from requests.packages.urllib3.util import Retry

I have urllib3 installed also (I've just installed it now via Pip). I am getting the error message:

Traceback (most recent call last):
  File "C:\Python27\counter.py", line 3, in <module>
    from requests.packages.urllib3.util import Retry
ImportError: cannot import name Retry

Can anyone tell me why this is? Are there any other dependencies I am unaware of to run this line of code successfully?

Thanks

gdogg371
  • 3,879
  • 14
  • 63
  • 107

2 Answers2

31

You might need a newer version of Requests. I just tried it with v2.5.1:

from requests.packages.urllib3.util import Retry

Seems to work. FYI: The latest version is v2.5.3, worth upgrading.

Also if you have a reasonably recent version of urllib3 installed separately, this should also work:

from urllib3.util import Retry

Unfortunately we check the specific isinstance type of Retry in PoolManager and ConnectionPool, so the two types of Retry objects might not be perfectly interchangeable. (If anyone wants to fix this, I'd be +1 on a PR.)

For now, if you're intending on using the Retry object with the requests version of urllib3, you'll need to import it from there directly.

shazow
  • 17,147
  • 1
  • 34
  • 35
  • 1
    i forgot to post an answer for this. i tried importing retry directly from urllib3 when i installed it a couple of days ago and it worked fine. thanks for the response though. – gdogg371 Mar 08 '15 at 19:55
  • 1
    @gdogg371 Ah good to know, thanks. It should be fine in some/many cases, but there may be edge cases where they're not perfectly interchangeable. Keep that in mind if you run into weird behaviour. :) – shazow Mar 09 '15 at 00:10
  • What's the minimum version? urllib3 1.7.1/requests 2.2.1 shows the error. – Nemo Jun 02 '16 at 10:49
21

requests no longer has vendored modules in request.package

you will need to reference urllib3 directly

from urllib3.util import Retry
Justin Poehnelt
  • 2,992
  • 1
  • 19
  • 23