I found the following line in Python code:
from six.moves import urllib
Simultaneously, I can find urllib.py
anywhere. I found that there is a file six.py
in package root and it has class Module_six_moves_urllib(types.ModuleType):
inside.
Is this it? How is this defined?
UPDATE
Sorry I am new to Python and the question is about Python syntax. I learned, that what is after import
is Python file name without a py
extension. So, where is this file in this case?
Urllib package is the URL handling module for python. It is used to fetch URLs (Uniform Resource Locators). It uses the urlopen function and is able to fetch URLs using a variety of different protocols.
httplib and httplib2 handles HTTP/HTTPs request and response directly and give you more space to do your own job. urllib and urllib2 are build upon httplib, they are more abstract and powerful, but sometimes won't fulfill your specified need about some HTTP related operations.
The urllib module in Python 3 allows you access websites via your program. This opens up as many doors for your programs as the internet opens up for you. urllib in Python 3 is slightly different than urllib2 in Python 2, but they are mostly the same.
The first thing that you’re going to have to do is you’re going to need to import URLlib. Now if you’re coming from python 2.7 you’re used to just import urllib and that’s it. Whereas with Python 3 and onward you will have to import the request package from the urllib. So an example of visiting a website will be as follows.
It uses the urlopen function and is able to fetch URLs using a variety of different protocols. Urllib is a package that collects several modules for working with URLs, such as: urllib.request for opening and reading.
It is intended to support codebases that work on both Python 2 and 3 without modification. six consists of only one Python file, so it is painless to copy into a project. Six can be downloaded on PyPI. Its bug tracker and code hosting is on GitHub. The name, “six”, comes from the fact that 2*3 equals 6.
This module helps to define functions to manipulate URLs and their components parts, to build or break them. It usually focuses on splitting a URL into small components; or joining different URL components into URL strings.
six is a package that helps in writing code that is compatible with both Python 2 and Python 3.
One of the problems developers face when writing code for Python2 and 3 is that the names of several modules from the standard library have changed, even though the functionality remains the same.
The six.moves
module provides those modules under a common name for both Python2 and 3 (mostly by providing the Python2 module under the name of the Python 3 module).
So your line
from six.moves import urllib
imports urllib
when run with Python3 and imports a mixture of urllib
, urllib2
and urlparse
with Python2, mimicking the structure of Python3's urllib
. See also here.
EDIT to address the update of the question:
TLDR; There is not necessarily a direct relation between the imported module urllib
and a file on the filesystem in this case. The relevant file is exactly what six.__file__
points to.
Third party modules are defined in a file/directory that is listed in sys.path
. Most of the time you can find the name of the file a module is imported from by inspecting the __file__
attribute of the module in question, e.g. six.__file__
. However with six.moves
things are not as simple, precisely because the exposed modules might not actually map one to one to actual Python modules but hacked versions of those.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With