What did Python call an unimported module?

I used:

import urllib.request 

And then:

 d={'value':12345} urllib.parse.urlencode(d) 

I expected python to throw an exception because I did not import urllib.parse, but it worked instead. What is the reason for this?

PS: I am using Python 3.3.3

+5
source share
1 answer

Here is the corresponding structure for urllib package in Python 3.3.3

 urllib/ __init__.py <-- empty file request.py parse.py 

There is a line next to the top of the request module, which looks like this:

 from urllib.parse import some, helper, functions 

And this line, which is the cause of urllib.parse , falls into scope.

You can verify that the implementation of import urllib.parse , on the other hand, does not cause the import of urllib.request . It will necessarily bind the name urllib (since urllib.parse is not a valid identifier by itself), but it will not import any other submodules.


Note. If you did one of the following:

 import urllib.request as urllib_dot_request from urllib import request 

Then you will have neither urllib nor urllib.parse available in the namespace. They will still be imported by the modular loader, but will not be included in the scope.

+3
source

Source: https://habr.com/ru/post/1200826/


All Articles