Cannot import requests.packages.urllib3.util 'Retry'
Asked Answered
P

2

31

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

Priapus answered 6/3, 2015 at 17:47 Comment(3)
possible duplicate of ImportError: Cannot import name XFinagle
@alfasin I cant see anything on that page that will assist me?Priapus
Check your imports - it suggests that you have circular dependenciesFinagle
S
34

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.

Serenata answered 6/3, 2015 at 22:11 Comment(3)
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.Priapus
@Priapus 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. :)Serenata
What's the minimum version? urllib3 1.7.1/requests 2.2.1 shows the error.Grecian
R
24

requests no longer has vendored modules in request.package

you will need to reference urllib3 directly

from urllib3.util import Retry
Rhodium answered 27/5, 2017 at 6:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.