I need to use 'requests' and a function from urllib3
. In the code
you can see the requests
library is being imported, but at the same time it is a module/package inside urllib3
, which has already been imported too.
Doing some research I've found that Python comes with the urllib
package, that comes with the request
module. On the other hand, requests
is a module inside urllib3
, but it is a library on its own.
urllib
and urllib2
are standard Python librares, but urllib3
is a completely separated library with a confusing name. A portion of it has been included in the standard library and requests
depends on it, but it is not a newer version of urllib
/urllib2
; the library that actually wants to improve is httplib
(ref: Github).
"Under the hood, requests uses urllib3 to do most of the http heavy lifting. When used properly, it should be mostly the same unless you need more advanced configuration"(ref: Stackexchange):
I got to these conclusions but I'm still confused: if I have already imported urllib
, do I still need to import requests
? What if I had imported urllib3
?
Also, should requests
be imported separately, as in the depicted code, or should it be import imported from one of the mentioned libraries?
If you are using the requests
module, then that's the only thing you need to import. The rest is taken care of for you by Python. That urllib3
is used by requests
is an implementation detail, unless you need to access specific objects defined by the urllib3
library you don't need to import that into your codebase.
requests
is not a module inside urllib3
. The urllib3
module defines a urllib3.request
(singular!) module, which is something completely different from the requests
package:
>>> import urllib3
>>> import requests
>>> urllib3.request
<module 'urllib3.request' from '/Users/mj/Development/venvs/stackoverflow-latest/lib/python3.7/site-packages/urllib3/request.py'>
>>> requests
<module 'requests' from '/Users/mj/Development/venvs/stackoverflow-latest/lib/python3.7/site-packages/requests/__init__.py'>
>>> requests is urllib3.request
False
Note that urllib3.request
is a module inside the urllib3
package namespace. import request
(no s
) would not import it, as that requests a top-level name. Neither would import somethingelse.request
, as that would be a different namespace from urllib3
. And just because the names requests
and request
are similar does not mean that they are the same thing. The HTTP standard deals with the abstract concept of a request sent to a server and the server giving back a response, so names in different projects dealing with HTTP will often reflect the concepts that they represent in code.
The same applies to the names urllib
and urllib2
. Python, once upon a time, started with a urllib
package that was limited, and later on a urllib2
package was added to that extended the first and added more functionality. Python 3 consolidated the two packages and reorganised the functionality into a series of new modules under the urllib
namespace. The urllib3
package is an independent project to build a better library for the same functionality. No parts of that project are incorporated into the Python standard library.
requests
is an project that gives you a clean, straightforward, easy to use API to make HTTP requests and handle the resulting responses. Use it for that API, don't worry about the implementation details.