Skip to content

Instantly share code, notes, and snippets.

@vadimkantorov
Last active January 3, 2018 23:12
Show Gist options
  • Select an option

  • Save vadimkantorov/c9f87278ba241bba10ccbecccc42a87d to your computer and use it in GitHub Desktop.

Select an option

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.
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)))
# 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