Last active
January 3, 2018 23:12
-
-
Save vadimkantorov/c9f87278ba241bba10ccbecccc42a87d to your computer and use it in GitHub Desktop.
A trick to make certain Python3 modules available in Python2 without modifying the original Python3 code. It suffices to place sitecustomize.py along with original code.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import sys | |
| import urllib | |
| import urlparse | |
| sys.modules['urllib.request'] = type('', (object, ), dict(urlretrieve = staticmethod(urllib.urlretrieve))) | |
| sys.modules['urllib.parse'] = type('', (object, ), dict(urlparse = staticmethod(urlparse.urlparse))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Usage: python2 test.py | |
| # Normally fails on Python2, but because of sitecustomize.py it will work fine | |
| from urllib.parse import urlparse | |
| from urllib.request import urlretrieve |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment