Reputation: 22008
I have the following code:
import urllib
import wget as wget
request = urllib.request.Request(url)
For the last line PyCharm shows a warning:
Cannot find reference 'request' in '__init__.py'
The code works finde though, I receive a reply from the server I query.
The import import wget as wget
is being shown as unused. When I remove that import, I get the following exception at runtime:
AttributeError: module 'urllib' has no attribute 'request'
Can somebody explain to me
urllib.request.Request
is obviously wrong(?) despite being documented here?Upvotes: 0
Views: 2112
Reputation: 33387
Because request is a submodule, you have to write its name so Python will also import it
import urllib.request
Reasoning:
If you have a module with many submodules, loading all of them might be an expensive thing to do and might create problems with things that are unrelated to your program.
Imagine there's a module called urllib.foo
that only works on Windows and you are working on Linux. If the import system were to import all submodules, it could crash on your system even if you don't want to use it.
The wget
module you are using imports the urllib.request
to use for itself, so python will cache its import and it will be part of the urllib
object.
Upvotes: 1
Reputation: 161
urllib
itself does not import urllib.request
. Module wget
probably does an import urllib.request
. From there on, urllib.request
actually is referenceable.
Suggested solution: write import urllib.request
, and then it should work without the import wget
.
Upvotes: 1